View mode: basic / threaded / horizontal-split · Log in · Help
March 11, 2010
Tidy attributes
While looking for possible attribute problems to add to Bugzilla, I have seen the following D2 program compiles and runs with no errors or warnings:


static foo1() {}
final foo2() {}
ref foo3() {}
enum void foo5() {}
nothrow foo4() {}
pure foo6() {}
static int x1 = 10;
static x2 = 10;
void main() {}


I don't like that code, but I don't know if it's correct.

- What is a static global function in D?
- A final global function?
- Is that ref of void correct? (I think it is not)
- A enum of void function?
- What are global static variables in D?
- Are most of those attributes supposed to not need the "void"?


The following lines don't compile, is this supposed to be correct? 

int static x3 = 10;
int enum x4 = 1;
int const x5 = 2;

Bye,
bearophile
March 11, 2010
Re: Tidy attributes
On 03/11/2010 10:20 PM, bearophile wrote:
> While looking for possible attribute problems to add to Bugzilla, I have seen the following D2 program compiles and runs with no errors or warnings:
>
>
> static foo1() {}

static does not apply to free functions, I would guess this means the 
same as auto.

> final foo2() {}
> ref foo3() {}
> enum void foo5() {}

Wow.

> nothrow foo4() {}
> pure foo6() {}

Would guess for auto here. Not tidy, as you say :)

> static int x1 = 10;
> static x2 = 10;

static here means 'known at compile time', I think.

> - What is a static global function in D?

It means nothing, but I can't seem to find the documentation. It's just 
ignored.

> - A final global function?
> - Is that ref of void correct? (I think it is not)
> - A enum of void function?

These are mysteries.

> - What are global static variables in D?

variables evaluated at compile time, for example

int cfte_able_function() {
    return 14423 + 12341;
}

int other_function() {
    writeln("Not cfte");
    return 7;
}

static x1 = 123; //works
static x2 = cfte_able_function; //works;
static x3 = other_function; //cannot evaluate other_function() at 
compile time

As you say, these are odd issues indeed. :)
March 11, 2010
Re: Tidy attributes
Pelle M.

> static does not apply to free functions, I would guess this means the 
> same as auto.

As far as I know a global function like:
auto foo() {}
is correct in D2, it means the compiler will infer the return type, that here is void. That's why I have not added this case to that list.


>static here means 'known at compile time', I think.

As far as I know, it's "enum" that has that purpose.


> As you say, these are odd issues indeed. :)

I'll wait for more answers, and then I'll add to Bugzilla. Java is not the language I prefer, but I like a lot the strictness of its compiler, it makes sure your attributes are all correct and meaningful. This avoids bugs and do helps newbies learn the language in less time.

Thank you for your answers,
bearophile
March 11, 2010
Re: Tidy attributes
On 03/11/2010 10:44 PM, bearophile wrote:
> As far as I know, it's "enum" that has that purpose.

Oh, but they are not the same. enum declares a constant, whereas static 
declares a variable. A static global is still mutable.

> Thank you for your answers,
> bearophile

Why thank you!
March 11, 2010
Re: Tidy attributes
Filed it, with some small changes:
http://d.puremagic.com/issues/show_bug.cgi?id=3934

Bye,
bearophile
March 12, 2010
Re: Tidy attributes
bearophile wrote:
> While looking for possible attribute problems to add to Bugzilla, I have seen the following D2 program compiles and runs with no errors or warnings:
> 
> 
> static foo1() {}
> final foo2() {}
> ref foo3() {}
> enum void foo5() {}
> nothrow foo4() {}
> pure foo6() {}
> static int x1 = 10;
> static x2 = 10;
> void main() {}
> 
> 
> I don't like that code, but I don't know if it's correct.
> 
> - What is a static global function in D?
> - A final global function?
> - Is that ref of void correct? (I think it is not)
> - A enum of void function?
> - What are global static variables in D?
> - Are most of those attributes supposed to not need the "void"?
> 
> 
> The following lines don't compile, is this supposed to be correct? 
> 
> int static x3 = 10;
> int enum x4 = 1;
> int const x5 = 2;
> 
> Bye,
> bearophile

I have discussed this subject many times, but it doesn't seem very 
important to the D dev team. IIRC they said it doesn't cause any harm.

But in some real code I have seen:

static int foo() { ... }

in global scope, and I always wondered why was that static there. Maybe 
the programmer thought static implied something there and now he's using 
it incorrectly, and now it confused me too and probably many others. So 
I think it is harmful because if the compiler allows such things than 
programmers can assume that must mean something.
March 15, 2010
Re: Tidy attributes
Ary Borenszweig wrote:
> bearophile wrote:
>> While looking for possible attribute problems to add to Bugzilla, I 
>> have seen the following D2 program compiles and runs with no errors or 
>> warnings:
>>
>>
>> static foo1() {}
>> final foo2() {}
>> ref foo3() {}
>> enum void foo5() {}
>> nothrow foo4() {}
>> pure foo6() {}
>> static int x1 = 10;
>> static x2 = 10;
>> void main() {}
>>
>>
>> I don't like that code, but I don't know if it's correct.
>>
>> - What is a static global function in D?
>> - A final global function?
>> - Is that ref of void correct? (I think it is not)
>> - A enum of void function?
>> - What are global static variables in D?
>> - Are most of those attributes supposed to not need the "void"?
>>
>>
>> The following lines don't compile, is this supposed to be correct?
>> int static x3 = 10;
>> int enum x4 = 1;
>> int const x5 = 2;
>>
>> Bye,
>> bearophile
> 
> I have discussed this subject many times, but it doesn't seem very 
> important to the D dev team. IIRC they said it doesn't cause any harm.
> 
> But in some real code I have seen:
> 
> static int foo() { ... }
> 
> in global scope, and I always wondered why was that static there. Maybe 
> the programmer thought static implied something there and now he's using 
> it incorrectly, and now it confused me too and probably many others. So 
> I think it is harmful because if the compiler allows such things than 
> programmers can assume that must mean something.

Please don't confuse "it's not a high priority" with "it won't be 
fixed". There are nearly 900 open compiler bugs in Bugzilla, and we can 
only fix a couple of bugs per day. Compiler faults and wrong code 
generation always get top priority.
Top | Discussion index | About this forum | D home