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

rikki cattermole rikki at cattermole.co.nz
Mon Dec 16 12:10:24 UTC 2019


On 17/12/2019 12:13 AM, sighoya wrote:
> On Monday, 16 December 2019 at 02:35:50 UTC, rikki cattermole wrote:
>> On 16/12/2019 9:07 AM, sighoya wrote:
>>>> That is the static introspection capabilities.
>>>> For dynamic:
>>>>
>>>> // typeof(input) is still templated, its just hidden, since only the 
>>>> template arguments need deducing
>>>> InputRange!ElementType map(alias func)(auto:InputRange input) if 
>>>> (...) {
>>>>     struct Voldemort {
>>>>         ...
>>>>     }
>>>>
>>>>     return ...; // auto construction of an InputRange instance with 
>>>> deduction of what ElementType is.
>>>> }
>>>
>>> Im not quite aware of the meaning of auto:InputRange but in my eyes 
>>> it is static dispatching too, I find it equivalent too:
>>>
>>> InputRange!ElementType map(alias func,ElemType)(InputRange!ElemType 
>>> input){...}
>>
>> Whatever map returns, can be initialized to the InputRange signature.
>> That is what auto:Signature means. Where Signature can be although 
>> doesn't have to be template initialized.
>>
>> I think this is quite nice syntax, as it works in the function 
>> parameters and it is easily recognized by the parser because keyword 
>> auto then ':' then some type.
>>
>> What you changed it to, is using runtime dispatch instead of static.
>> Another form I've come up with for static is ``is(T : Signature)``. 
>> With same meaning as ``auto:Signature`` but requires template 
>> constraints to mean the same thing.
> 
> Ah okay, so this is a specialization of the auto kw which is generic T 
> implying T:Signature

Yeah, basically it tells the reader that the return value is guaranteed 
to be X. A small but what I think would be a very nice documentation 
addition especially for Phobos which is hard to get into.


More information about the Digitalmars-d mailing list