Is there any chance to introduce concepts/traits/typeClasses in dlang?

sighoya sighoya at gmail.com
Sun Dec 15 20:04:45 UTC 2019


> Looks to me like this is just a way of attaching D's static
> constraints to a type, so that
>
>   void push(T)(Stack!T stack, T item) { }
>
> becomes (something like, for inference's sake S shouldn't be
> separate I think):
>

>   void push(S,T)(S!T stack, T item) if (a bunch of !S tests) { }
>

The point is that this only works for:

1.) static dispatch (compile time dispatch)
2.) for structural matches (is there any kind to constraint a 
type on its ufcs/assoc methods?), structural methods can't be 
added after the type is defined, however ufcs/assoc/extension 
methods can be defined later and pulled automatically into 
function scope of the method taking the implementing type of the 
concept:

void fun(C:Concept)(C c)
<==>
void fun(C)(C c) if (C has all ufcs/assoc functions listed in the 
concept C)

How to express the latter?

> What does this have to do with dynamic dispatch? How is this
> not already something you can do with an isStack!S test?

Dynamic dispatch mean you have an runtime uncertainty of the 
implementing type of a concept like the implementing type of a 
interface, so you can write:

fun(Concept c) akin to fun(Interface i) which is different to:

fun(C:Concept)(C c)

as the latter method-set for the implementing type is passed at 
compile time and for the former at runtime.






More information about the Digitalmars-d mailing list