encore icon indicating copy to clipboard operation
encore copied to clipboard

Primitive types should be able to implement traits

Open EliasC opened this issue 7 years ago • 1 comments

We use traits to be able to bound polymorphic types to certain interfaces. For example, you might want to write a function like the following

fun foo[t](x : Eq[t], y : Eq[t]) : bool
  x.eq(y)
end

Currently however, only classes can implement traits, so foo cannot be called with two integers. The smoothest way to implement this would be to have some way of allowing primitive types to implement a trait. One could go the way of Java and have boxed versions of all the primitives, but that would introduce overhead when using primitives in a polymorphic context. On the other hand, without boxing one would have to check dynamically if a polymorphic value is a reference type (which carries its own vtable) or a primitive (whose vtable would need to be stored elsewhere).

This is related to #797.

EliasC avatar May 04 '17 12:05 EliasC

Isn't there something in @kaeluka's Iter PR for adding traits to existing classes (like Fut)? Maybe this can be used.

supercooldave avatar May 04 '17 14:05 supercooldave