Embedded Containers

Timon Gehr timon.gehr at gmx.ch
Tue Dec 5 20:38:01 UTC 2017


On 05.12.2017 20:11, H. S. Teoh wrote:
> On Tue, Dec 05, 2017 at 07:09:50PM +0000, Adam D. Ruppe via Digitalmars-d wrote:
>> On Tuesday, 5 December 2017 at 19:01:48 UTC, A Guy With a Question wrote:
>>> alias Items(T) = Array!Item(T);
>>
>> try:
>>
>> Array!(Item!(T))
>>
>>> I'm not quite sure I understand how to create a generic container
>>> interface or class in D.
>>
>> Just using the parenthesis should help. The thing with A!B!C is that
>> the reader can easily be confused: did you mean A!(B!C) or (A!B)!C or
>> what? Now the compiler is a bit stupider about this than it should be
>> IMO - it should be able to figure it out and the meaning be fairly
>> clear with association - but it isn't, so you can and must write
>> parens to clarify most the time.
> 
> Here's an idea for a DIP: make '!' right-to-left associative (i.e.,
> similar to the ^^ exponentiation operator), so that A!B!C is understood
> as A!(B!C).
> 
> Rationale: the most common use cases are of the A!(B!C) variety; it's
> pretty rare IME to need the (A!B)!C form, since usually a template
> expands to a type, which can then be passed to another template, i.e.,
> A!(B!C).  The (A!B)!C form is when the template instantiated with B
> produces another template that takes another type argument.  There
> aren't many use cases for this that I can think of.
> ...

Curried templates are actually common enough in Phobos. (map, filter, etc.)

> Though the point is probably moot, because in the current grammar you'd
> need parentheses as soon as the template argument is anything more than
> a single token, i.e., you can write A!int but you have to write
> A!(const(int)).  And in the cases where you actually want something of
> the form A!(B!C), usually the arguments are themselves pretty
> complicated, so wouldn't benefit from top-level associativity anyway.
> 
> 
> T
> 

IMHO the inconsistency with function call syntax ought to kill this 
proposal. Also, it is a bit more confusing than it is useful.

Related: It's quite annoying that (A!B)!C does not work because the 
parser thinks it's a C-style cast and errors out even though C-style 
casts are not even valid D syntax.


More information about the Digitalmars-d mailing list