const debacle

Janice Caron caron800 at googlemail.com
Fri Mar 21 22:44:30 PDT 2008


On 21/03/2008, Walter Bright <newshound1 at digitalmars.com> wrote:
> There's been some discussion about this, but no good resolution.

Allow me to offer a suggestion.

There are several stages to this reasoning, but I think you're going
to like the final deduction at the end. Stage one: allow the word
"auto" wherever "const" and "invariant" are currently allowed. The
meaning of "auto" is always "do nothing". This is consistent with the
documented meaning: "The auto attribute is used when there are no
other attributes and type inference is desired." - we're just
extending it beyond type inference. So in particular:

    auto(T)

becomes an allowable do-nothing type constructor, so that auto(T) is
just an alias for T. And also, since we can declare functions like

    void f(const int x)

then we must also allow

    void f(auto int x)

again, with auto being a "do nothing" keyword, so the latter is
completely equivalent to

    void f(int x)

(Note: I am /not/ suggesting that "void f(auto x)" be allowed as a
function signature. In fact, it won't be, since "void f(const x)" is
not allowed.)

Why am I suggesting this? Well, bear with me. All will become clear
shortly. The next step is to introduce a new kind of template
parameter:

    template T(const K)
    {
        ...
    }

The idea is that the placeholder K may be substituted with one of
exactly three symbols: "auto", "const" and "invariant". Thus, we may
now write:

    template T(const K)
    {
        K char[] f(K char[] buf ) { ... }
    }

and instantiate it with each of:

    T!(auto).f(buf)
    T!(const).f(buf)
    T!(invariant).f(buf)

This expands out to the three functions

    auto char[] f(auto char[] buf ) { ... }
    const char[] f(const char[] buf ) { ... }
    invariant char[] f(invariant char[] buf ) { ... }

and since "auto" means "do nothing", the first of these reduces to

    char[] f(char[] buf ) { ... }

Next, we use the simplified template declaration syntax for functions,
to do exactly the same thing:

    K char[] f(const K)(K char[] buf ) { ... }

and instantiate with

    f!(auto)(buf)
    f!(const)(buf)
    f!(invariant)(buf)

And the final step - allow type deduction, so that we now only have to write

    f(buf)

and have the compiler choose the right instantiation based on the
constancy of buf.

This, I think, would be problem solved.



More information about the Digitalmars-d mailing list