I come from OO languages ββand am currently trying to develop an application in Haskell. I am trying to use what would be an abstract class in OO languages, but this does not seem to be suitable for the Haskell type system (and probably most functional languages).
In, say, Java, the minimal code of what I'm trying to express is likely to look something like this.
abstract class Transformation {
public abstract void transform(Image image);
}
class ResizeTransformation extends Transformation {
public void transform(Image image) {
}
}
class Worker {
public void applyTransformations(Image image, Transformation[] trs) {
for (Transformation t: trs) {
t.transform(image);
}
}
}
class Parser {
public Transformation parseTransformation(String rawTransformation) {
}
}
I tried to express this in Haskell with a class Transformationand create multiple instances for each transformation, but I was stuck in Parser, since it seems impossible to return an abstract type.
Here is more or less what my Haskell code looks like:
class Transformation a where
transform :: Image -> a -> Image
data Resize = Resize
{ newHeight :: Int
, newWidth :: Int
} deriving (Show)
instance Transformation Resize where
transform image resize = -- resize the image
applyTransformations :: (Transformation a) => Image -> [a] -> Image
applyTransformations image transformations = foldl transform image transformations
And I would like to have a function with such a signature
parseTransformation :: (Transformation a) => String -> Maybe a
, , Maybe Resize could not deduce a ~ Resize, .
- , - Haskell.
, , , Haskell.