Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymorphic callbacks in data constructor

Tags:

haskell

I'm just starting to learn haskell. I have custom class

class MyClass a where
  doSomething :: a -> ()

Can I write a data with polymorphic callback in constructor?

data MyData = MyConstructor {
  callback :: (MyClass m) => m -> () -- error
}

I want to make constraint for function argument.

like image 765
brainstream Avatar asked Dec 05 '25 18:12

brainstream


1 Answers

In short, Yes you can by using Rank2Types or RankNTypes pragma.

As your data type is existentially quantified. It is actually

data MyData = MyConstructor (forall m. MyClass m => m -> ())

for which you require higher rank extension.

like image 97
Satvik Avatar answered Dec 08 '25 00:12

Satvik