Configurable syntax

bearophile bearophileHUGS at lycos.com
Fri May 22 07:28:14 PDT 2009


Andrei Alexandrescu:

> 1. Allow inner name promotion even if the template defines other
> members, as long as they are all private:
>
> now:
>
> template WidgetImpl(A, B) {
>      ...
>      alias ... Result;
> }
>
> template Widget(A, B) {
>      alias WidgetImpl!(A, B) Widget;
> }
>
> proposed:
>
> template Widget(A, B) {
> private:
>      ...
>      alias ... Result;
> public:
>      alias Widget Result;
> }
>
> It's needed very frequently, puts sand in the eye, and almost sure to
> throw off the casual reader.

The D1 documentation says:

>Implicit Template Properties: If a template has exactly one member in it, and the name of that member is the same as the template name, that member is assumed to be referred to in a template instantiation:<

Two possible ideas:

1) If it contains a member with the name of the template (and other names), it can be referred anyway with the name of the template:

template Foo(T) {
    T Foo;
    T[] Bar;
}

void test() {
    Foo!(int) = 6; // equal to Foo!(int).Foo anyway
}

This may lead to troubles I am not seeing yet.

----------------

2) Having private and public members as in your example solves the problem, but it may be overkill, because in many situations you have only one public name, plus some private ones. So a 'return' may be used:

template WidgetImpl(A, B) {
     T[] Bar;
     T Foo;
     return Foo;
}

template WidgetImpl(A, B) {
    ...
    return Result;
}

This doesn't allow you to hide names from the outside, but helps you avoid having a second template.

Having a return also helps to see templates as closer to compile-time functions (changing few things there may be ways to merge the syntax of templates with the syntax of compile time functions, reducing the complexity of D).

Bye,
bearophile



More information about the Digitalmars-d mailing list