Haskell custom data type with parameter

advertisements

I have a custom data type as follows:

 data MyType a = Nothing
             | One a

The idea is to have a function that return the data type. For example,

func (One Char) should return Char -- return the data type

I tried to implement func as follow:

func :: MyType a -> a
func (One a) = a
-- don't worry about Nothing for now

The code compiled but when I tried to run func (One Char) it gives me an error: Not in scope: data constructor ‘Char’

What is going on?


Try func (One "Hello") it will work.

The reason is that Haskell thinks you are trying to give it a data-constructor because you wrote a upper-case Char so just give it some real value ;)

BTW: In case you really want to give the type Char: that's in general not possible in Haskell as you need a dependent-type caps (see Idris for example).