effekt icon indicating copy to clipboard operation
effekt copied to clipboard

Typeclasses

Open b-studios opened this issue 2 years ago • 3 comments

We should consider adding typeclasses or similar to prevent repeating code such as sorting datastructures, etc.

b-studios avatar Apr 15 '22 18:04 b-studios

This seems to me like a straightforward transformation to codata:

  • class definition -> interface definition
  • instance -> top level object
  • type constraints -> implicit parameters

I guess the tricky part would be inferring the correct instance.

serkm avatar Nov 04 '23 15:11 serkm

A suggestion for syntax:

class C[A] {
    // function declarations
}

instance C[T] {
    // function definitions
}

// type constraints
def f [A] <C[A]> (a: A) = {...}

serkm avatar Nov 04 '23 15:11 serkm

Another idea would be to add implicit parameters to Effekt and use regular codata to express type classes:

// implicit parameter of type A
def f () <a: A> = {...}

def main() = {
   f() // passes implicit value iff there is exactly one value of type A in scope
}

serkm avatar Nov 04 '23 15:11 serkm