1

I have a class :

> class Binaer a where
>   binaer_zu_a :: String -> a

And two instances:

> instance Binaer Integer where
>   binaer_zu_a b = 3

> instance Binaer Bool where
>   binaer_zu_a b = True

So the input to he function binaer_zu_a is always a string, but how do I have to call the function so that the function "knows" which of them it should execute?

If I try to call it like binaer_zu_a :: Bool "01" for example I ge anERROR - Syntax error in input (unexpected string literal)

So how do I need to call that function?

1
  • If you annotate the function, you need to do so with a function type: (binaer_zu_a :: String -> Bool) "True". Commented Nov 3, 2019 at 16:41

2 Answers 2

9

Specify the type argument like this

> (binaer_zu_a "b") :: Integer
3
> (binaer_zu_a "b") :: Bool
True
Sign up to request clarification or add additional context in comments.

Comments

3

With the TypeApplications extension, you can specify the type (or rather, an expression representing the type) as the first argument.

> :set -XTypeApplications
> binaer_zu_a @Bool "True"
True

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.