www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Cannot alias null

reply Tom Browder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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
Jun 12 2014
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Jun 12 2014
parent reply Andrew Edwards <ridimz yahoo.com> writes:
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?
Jun 12 2014
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Jun 12 2014
prev sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
since null is a value maybe you want

enum blah = null;

you may also give it a type after the enum word
Jun 12 2014
next sibling parent Tom Browder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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
Jun 12 2014
prev sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
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.
Jun 12 2014