Thread overview
Cannot alias null
Jun 12, 2014
Tom Browder
Jun 12, 2014
Ali Çehreli
Jun 12, 2014
Andrew Edwards
Jun 12, 2014
Ali Çehreli
Jun 12, 2014
Adam D. Ruppe
Jun 12, 2014
Tom Browder
Jun 12, 2014
monarch_dodra
June 12, 2014
This will not compile:

  alias blah = null;

The dmd message are:

di/test_hdr.d(10): Error: basic type expected, not null
di/test_hdr.d(10): Error: semicolon expected to close alias declaration
di/test_hdr.d(10): Error: Declaration expected, not 'null'

Are there any other objects that cannot be aliased?

Thanks,

Best,

-Tom
June 12, 2014
On 06/12/2014 01:26 PM, Tom Browder via Digitalmars-d-learn wrote:

> This will not compile:
>
>    alias blah = null;
>
> The dmd message are:
>
> di/test_hdr.d(10): Error: basic type expected, not null
> di/test_hdr.d(10): Error: semicolon expected to close alias declaration
> di/test_hdr.d(10): Error: Declaration expected, not 'null'
>
> Are there any other objects that cannot be aliased?

alias works only with types. Being an expression (not an object), null cannot not work with alias.

Ali

June 12, 2014
On 6/12/14, 4:29 PM, Ali Çehreli wrote:
> On 06/12/2014 01:26 PM, Tom Browder via Digitalmars-d-learn wrote:
>
>  > This will not compile:
>  >
>  >    alias blah = null;
>  >
>  > The dmd message are:
>  >
>  > di/test_hdr.d(10): Error: basic type expected, not null
>  > di/test_hdr.d(10): Error: semicolon expected to close alias declaration
>  > di/test_hdr.d(10): Error: Declaration expected, not 'null'
>  >
>  > Are there any other objects that cannot be aliased?
>
> alias works only with types. Being an expression (not an object), null
> cannot not work with alias.
>
> Ali
>

void foo() {}
alias bar = foo();

Am I just misunderstanding what is meant by types?
June 12, 2014
On 06/12/2014 01:36 PM, Andrew Edwards wrote:

> void foo() {}
> alias bar = foo();
>
> Am I just misunderstanding what is meant by types?

Seems to be an old behavior. That does not compile with 2.066:

Error: function declaration without return type. (Note that constructors are always named 'this')

The following compiles though:

alias bar = foo;

I stand corrected: alias works not only with types but with symbols as well. I was right about the original code though: "Aliases cannot be used for expressions".

Ali

June 12, 2014
since null is a value maybe you want

enum blah = null;

you may also give it a type after the enum word
June 12, 2014
On Thu, Jun 12, 2014 at 4:58 PM, Adam D. Ruppe via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:
> since null is a value maybe you want
>
> enum blah = null;

That works.

> you may also give it a type after the enum word

But I can't get any other variant to work so far.

-Tom
June 12, 2014
On Thursday, 12 June 2014 at 21:58:32 UTC, Adam D. Ruppe wrote:
> since null is a value maybe you want
>
> enum blah = null;
>
> you may also give it a type after the enum word

I *think* the issue might be that "null" is an rvalue? Because you can alias variable names all you want. I do it all the time for templates where I *may* need a temporary.

eg:

void foo(T)(T val)
{
    static if (isUnsigned!T)
        alias uval = val;
    else
        auto uval = unsigned(val);
    ...
}

It's also quite useful with varargs:
alias a0 = args[0];

Also, you can't alias things like "int.init" either. I'm not sure the "rvalue" thing is the source, because these work:

//----struct S
{
    static int i;
    static int j() @property;
}
alias a = S.i;
alias b = S.j;
//----

I'd consider filling a bug report.