How do you define an object as 1?

In haskell 1 :: Num a => a

.

How could I (if by any means) replicate this behavior i.e. define a class MyNum

(possibly types and instances) and an object One

that has a type One :: MyNum a => a

?

+3


source to share


1 answer


class MyNum a where
  one :: a

      

Now one :: MyNum a => a

. Then you can write

instance MyNum Int where
  one = 1

      



etc.

Not sure what you are actually trying to achieve or if this is just a toy experiment or something. But I think this answers the question asked literally.

+4


source







All Articles