Template instantiation syntax
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Sat Oct 11 13:13:29 PDT 2008
Dave wrote:
>
> "Walter Bright" <newshound1 at digitalmars.com> wrote in message
> news:gcogl4$28ui$1 at digitalmars.com...
>> We seem to have reached a dead end on finding a significantly better
>> alternative than foo!(bar).
>>
>> All is not lost, though. Andrei is working on an emacs module that
>> will parse D code and replace foo!(bar) with foo«bar» for display only
>> when the editor is in D mode, the underlying text will still be
>> foo!(bar). (This doesn't affect D at all, only its display in Emacs.)
>>
>> Also, we're going to try using ! for single argument syntax, as in:
>>
>> foo!bar is same as foo!(bar)
>> foo!10 is same as foo!(10)
>>
>> etc. 0 arguments or more than 1 argument or arguments that are more
>> than one token long will still require !( ). We'll see how that works.
>> I think it looks rather nice.
>
> I don't like it because it is not consistent and therefore could make
> things ugly and even harder to comprehend where clarity is needed most
> -- in a file full of mixed length template instantiations. It reeks of
> total hack to me, and I think this is opening a huge can of worms
> regarding the a!b!c issue. Inconsistency and things that smack of
> potential "corner case" are never good.
>
> I hadn't seen this mentioned lately; C#, Java and now C++0x are able to
> work around the '> >' issue and allow '>>'.
It doesn't quite deserve much mentioning. C++ has had at best a Pyrrhic
victory with accommodating ">>" (the cost was a keyword, a special
syntax, and a few special syntax cases). Java's templates are
emasculated to begin with, and ">>" makes sure they won't easily become
more powerful.
> a) It works for C++0x so it can be made to work for D using the same
> rules. I also assume that C# and Java compilers use the same methods as
> C++0x to work around the ambiguities with the right-shift operator.
The methods are very different and that is because Java/C# generics are
less powerful.
> b) Easier and more natural for D newbies who've used C++ templates
> and/or C# / Java Generics.
Does the C++ construct
a.template foo<b>(c);
strike you as particularly natural? Do you know when you need to use it
and when not? In all honesty, did you ever know it exists?
> c) There can be no D code where ">>" instead of "> >" breaks existing
> code like C++98 vs C++0x.
That is incorrect.
> d) There wouldn't be the "I hate it because it's not C++ enough" first
> impression for templates.
I think the exclamation "it's not C++ enough" usually comes with a sigh
of relief.
> e) Less typing compared to current.
> f) Could make definition and instantiation consistent as well:
> class C<T> {T val;}
> auto c = new C<int>;
That comes bundled with making writing of a D parser essentially impossible.
> g) I'm sure the same workaround could be used for D's UShr operator '>>>'.
No. That will only make things another order of magnitude harder.
> h) Andrei will like it better because it's closer to chevrons <g>
Chevrons are not ">". They do pair.
> Also, as long as I'm thinking out of the box here, for D2 how about
> considering using a different group of symbols for bitwise shift (if for
> some reason the ambiguity issue is too large to work around)?
It would also need to use a different symbol for less-than and
greater-than. But Fortran's .LT. and .GT. don't seem that enticing.
> Before you yell "Sacralidge!" and roll your eyes, consider that out of
> about 100K lines of D code in phobos, 381 have "<<|>>" and 1711 have
> "!(.*)". Also, '>>>' is only used 25 times in phobos and could be
> replaced with '>>' and a cast where it is used.
I agree that '>>>' should go. The compiler can simply use the type of
the operand to disambiguate signed vs. unsigned shift. I'm glad you
reminded me to remind Walter of that.
> Perhaps ~> (shr), ~< (shl), ~>> (ushr), ~<= (shlAssign) and ~>=
> (shrAssign)?
I like the shr :o).
> Just a thought.
>
> Still there may be an issue with a<b,c>d, but again that is apparently
> solved in C#, Java and C++0x, and I didn't find any of that used in
> phobos. I think the way they solved this in C# at least was to disallow
> the comma operator between conditional predicates, which I don't see as
> a huge issue.
I think you'll find it very instructive to familiarize yourself with the
ways C++, Java, and C# accommodate use of <> as brackets.
Andrei
More information about the Digitalmars-d
mailing list