You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
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
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.
The text was updated successfully, but these errors were encountered: