Re: Cannot alias null

2014-06-13 Thread via Digitalmars-d-learn
On Thursday, 12 June 2014 at 21:07:47 UTC, Tom Browder via 
Digitalmars-d-learn wrote:
What I was really trying to do was D'ify C expressions like 
this:


  typedef ((struct t*)0) blah;


This doesn't compile for me with GCC, and I don't know what it's 
supposed to mean. ((struct t*) 0) is a value, not a type...


Where does it come from?


Re: Cannot alias null

2014-06-13 Thread monarch_dodra via Digitalmars-d-learn

On Thursday, 12 June 2014 at 22:54:20 UTC, Ali Çehreli wrote:

On 06/12/2014 03:38 PM, monarch_dodra wrote:
 So there's something special about null.

The difference is that null is an expression. It is the same 
limitation as not being able to alias a literal.


alias zero = 0;
alias blah = null;


Oh! Right. That makes sense.

So you should use enum instead:
enum zero = 0;
enum blah = null;

Thanks.


Re: Cannot alias null

2014-06-13 Thread Tom Browder via Digitalmars-d-learn
On Fri, Jun 13, 2014 at 7:59 AM, via Digitalmars-d-learn
digitalmars-d-learn@puremagic.com wrote:
 On Thursday, 12 June 2014 at 21:07:47 UTC, Tom Browder via
 Digitalmars-d-learn wrote:

 What I was really trying to do was D'ify C expressions like this:

   typedef ((struct t*)0) blah;


 This doesn't compile for me with GCC, and I don't know what it's supposed to
 mean. ((struct t*) 0) is a value, not a type...

Sorry, you're correct.  It is from a C macro and would be used for an
rvalue.  Something like this:

$ cat chdr.h
struct t;
#define t_nullptr ((struct t*)0)
struct t* t_ptr = t_nullptr;

After pre-processing with gcc -E -P that should read:

$ cat chdr.h.i
struct t;
struct t* t_ptr = ((struct t*)0);

which does compile.

So I'm not sure how to translate that into D.   I do know my first
attempt here doesn't work, even with it being surrounded by extern (C)
{}:

$ cat chdr.d
struct t;
struct t* t_ptr = null;

 Where does it come from?

The usage comes from many of the C API headers in the BRL-CAD package
(http://brlcad.org).

Best,

-Tom


Re: Cannot alias null

2014-06-13 Thread Philpax via Digitalmars-d-learn
On Friday, 13 June 2014 at 15:05:49 UTC, Tom Browder via 
Digitalmars-d-learn wrote:

On Fri, Jun 13, 2014 at 7:59 AM, via Digitalmars-d-learn
digitalmars-d-learn@puremagic.com wrote:

On Thursday, 12 June 2014 at 21:07:47 UTC, Tom Browder via
Digitalmars-d-learn wrote:


What I was really trying to do was D'ify C expressions like 
this:


  typedef ((struct t*)0) blah;



This doesn't compile for me with GCC, and I don't know what 
it's supposed to

mean. ((struct t*) 0) is a value, not a type...


Sorry, you're correct.  It is from a C macro and would be used 
for an

rvalue.  Something like this:

$ cat chdr.h
struct t;
#define t_nullptr ((struct t*)0)
struct t* t_ptr = t_nullptr;

After pre-processing with gcc -E -P that should read:

$ cat chdr.h.i
struct t;
struct t* t_ptr = ((struct t*)0);

which does compile.

So I'm not sure how to translate that into D.   I do know my 
first
attempt here doesn't work, even with it being surrounded by 
extern (C)

{}:

$ cat chdr.d
struct t;
struct t* t_ptr = null;


Where does it come from?


The usage comes from many of the C API headers in the BRL-CAD 
package

(http://brlcad.org).

Best,

-Tom


Remove the struct from the pointer:

struct t;
t* t_ptr = null;


Re: Cannot alias null

2014-06-13 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 13 June 2014 at 15:05:49 UTC, Tom Browder via 
Digitalmars-d-learn wrote:
So I'm not sure how to translate that into D.   I do know my 
first
attempt here doesn't work, even with it being surrounded by 
extern (C)

{}:

$ cat chdr.d
struct t;
struct t* t_ptr = null;


This seems to work fine for me. What's the problem?

Note this isn't *strictly* the same as what you have in C, as 
null is not strongly bindded to a single type. If you want a 
null pointer which is pre-emptivelly strongly, then you can use 
an eponymous template:


enum nullptr(T) = (T*).init;

auto p = nullptr!t;

Throw in an alias, and you got it working exactly like an C:


enum nullptr(T) = (T*).init;
alias t_nullptr = nullptr!t;
struct t* t_ptr = t_nullptr;

That said, I'd advise against this. Just use null and move on. 
It's idiomatic.


Re: Cannot alias null

2014-06-13 Thread Tom Browder via Digitalmars-d-learn
On Fri, Jun 13, 2014 at 10:15 AM, monarch_dodra via
Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote:
 On Friday, 13 June 2014 at 15:05:49 UTC, Tom Browder via Digitalmars-d-learn
 wrote:

 So I'm not sure how to translate that into D.   I do know my first
 attempt here doesn't work, even with it being surrounded by extern (C)
 {}:

 $ cat chdr.d
 struct t;
 struct t* t_ptr = null;


 This seems to work fine for me. What's the problem?

I use dmd -c .d (note file name change) and get:

t.d(2): Error: { } expected following aggregate declaration
t.d(2): Error: Declaration expected, not '*'

Taking Philpax's  suggestion I try

$ cat t.d
struct t;
t* t_ptr = null;

$ dmd -c t.d

and get a good build.

 Note this isn't *strictly* the same as what you have in C, as null is not
...
 strongly bindded to a single type. If you want a null pointer which is
 pre-emptivelly strongly, then you can use an eponymous template:
...
 That said, I'd advise against this. Just use null and move on. It's
 idiomatic.

So this is the correct (i.e., good enough) solution then:

$ cat t.d
struct t;
t* t_ptr = null;

Sounds good to me.

Thanks all.

Best,

-Tom


Re: Cannot alias null

2014-06-12 Thread Ali Çehreli via Digitalmars-d-learn

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



Re: Cannot alias null

2014-06-12 Thread Andrew Edwards via Digitalmars-d-learn

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?


Re: Cannot alias null

2014-06-12 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Jun 12, 2014 at 03:26:13PM -0500, Tom Browder via Digitalmars-d-learn 
wrote:
 This will not compile:
 
   alias blah = null;
[...]

'null' is a value, not a type. Try:

alias blah = typeof(null);


T

-- 
If it's green, it's biology, If it stinks, it's chemistry, If it has numbers 
it's math, If it doesn't work, it's technology.


Re: Cannot alias null

2014-06-12 Thread Ali Çehreli via Digitalmars-d-learn

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



Re: Cannot alias null

2014-06-12 Thread Tom Browder via Digitalmars-d-learn
On Thu, Jun 12, 2014 at 3:42 PM, H. S. Teoh via Digitalmars-d-learn
digitalmars-d-learn@puremagic.com wrote:
 On Thu, Jun 12, 2014 at 03:26:13PM -0500, Tom Browder via Digitalmars-d-learn 
 wrote:
 This will not compile:

   alias blah = null;
 [...]

 'null' is a value, not a type. Try:

 alias blah = typeof(null);

Great, that works!

What I was really trying to do was D'ify C expressions like this:

  typedef ((struct t*)0) blah;

So, taking your advice, I found this to work (at least it compiles as
a translation:

  alias blah = typeof(null);

Thanks,T and Ali.

Best,

-Tom


Re: Cannot alias null

2014-06-12 Thread Ali Çehreli via Digitalmars-d-learn

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

 What I was really trying to do was D'ify C expressions like this:

typedef ((struct t*)0) blah;

Is that actually a function pointer typedef? I can't parse that line. :)

 So, taking your advice, I found this to work (at least it compiles as
 a translation:

alias blah = typeof(null);

I suspect you need something else. :)

Ali



Re: Cannot alias null

2014-06-12 Thread Tom Browder via Digitalmars-d-learn
On Thu, Jun 12, 2014 at 4:17 PM, Ali Çehreli
digitalmars-d-learn@puremagic.com wrote:
 On 06/12/2014 02:06 PM, Tom Browder via Digitalmars-d-learn wrote:
...
 What I was really trying to do was D'ify C expressions like this:

typedef ((struct t*)0) blah;
...
 So, taking your advice, I found this to work (at least it compiles as
 a translation:

alias blah = typeof(null);

 I suspect you need something else. :)

Undoubtedly, indeed!  [Still a WIP (work in progress).]

Best,

-Tom



Re: Cannot alias null

2014-06-12 Thread Adam D. Ruppe via Digitalmars-d-learn

since null is a value maybe you want

enum blah = null;

you may also give it a type after the enum word


Re: Cannot alias null

2014-06-12 Thread Tom Browder via Digitalmars-d-learn
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


Re: Cannot alias null

2014-06-12 Thread monarch_dodra via Digitalmars-d-learn
On Thursday, 12 June 2014 at 20:44:16 UTC, H. S. Teoh via 
Digitalmars-d-learn wrote:
On Thu, Jun 12, 2014 at 03:26:13PM -0500, Tom Browder via 
Digitalmars-d-learn wrote:

This will not compile:

  alias blah = null;

[...]

'null' is a value, not a type. Try:

alias blah = typeof(null);


T


Yet you can alias variables...

int i;
alias j = i;

So there's something special about null.


Re: Cannot alias null

2014-06-12 Thread monarch_dodra via Digitalmars-d-learn

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.


Re: Cannot alias null

2014-06-12 Thread Ali Çehreli via Digitalmars-d-learn

On 06/12/2014 03:38 PM, monarch_dodra wrote:

 Yet you can alias variables...

 int i;
 alias j = i;

Initially I forgot about the fact that symbols can be alias'ed as well. 
So that's fine.


 So there's something special about null.

The difference is that null is an expression. It is the same limitation 
as not being able to alias a literal.


alias zero = 0;
alias blah = null;

Those two declarations fail for the same reason:

  Error: basic type expected, not 0
  Error: semicolon expected to close alias declaration
  Error: basic type expected, not null
  Error: semicolon expected to close alias declaration

The pair of error messages are somewhat silly: The first one is 
misleading because as we know, it should say basic type *or symbol* 
expected; and the second one is bogus because there actually is a 
semicolon there: :p


Ali