Static method in Haskell

Find this useful? Support us: Star on GitHub 6
Category: Class | Language: Haskell

In Haskell, you can define a static method in a class by using the class keyword followed by the class name, then the method name and its signature. Then, you can define the method implementation using the instance keyword and the class name followed by the method name and the implementation.

Here's an example:

class MyStaticClass a where
  myStaticMethod :: a -> Int

instance MyStaticClass Int where
myStaticMethod x = x + 1

instance MyStaticClass Bool where
myStaticMethod x = if x then 1 else 0

In this example, we define a static class MyStaticClass with a static method myStaticMethod that takes an argument of type a and returns an Int. We then define two instances of this class for two different types, Int and Bool, and provide implementations for the static method for each of them.

In the first instance, we implement the static method for the Int type by returning the value of the argument plus one. In the second instance, we implement the method for the Bool type by returning one if the argument is True and zero otherwise.

To use these static methods, you can simply call them using the class name and the syntax classMethod argument:

main = do
  print $ MyStaticClass.myStaticMethod (5 :: Int)
  print $ MyStaticClass.myStaticMethod True

This will output:

6
1