5

I have a typeclass that uses functional dependency and example instance:

class (KnownSymbol name) => MyClass name result | name -> result

instance MyClass "something" Int

How can I reuse this and get a type-level function GetType so that GetType "something" returns Int?

My unsuccessful attempt was pretty naive:

class CanGetType a b | a -> b where
  type GetType a

instance (MyClass name result) => CanGetType name result where
  type GetType name = result

which fails:

The RHS of an associated type declaration mentions out-of-scope variable ‘a’
  All such variables must be bound on the LHS
Tsvetan Ovedenski
  • 3,328
  • 1
  • 16
  • 9
  • 2
    Though this is indeed a duplicate, I find the answer to that question hardly satisfying. Added a bounty there which will hopefully result in better ones. – leftaroundabout Jun 01 '23 at 13:59
  • I think this is a difficult question to answer properly because the simple answer is "it's not possible", but there's most likely a workaround for any specific thing you are actually trying to do. It's an XY problem except that, unlike typical XY problems, the Y problem is quite natural and theoretically interesting IMO. So you might have better luck with an answer by providing details about your use case. – Li-yao Xia Jun 02 '23 at 11:32

0 Answers0