www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Can someone explain why this is not an error?

reply Bernard Helyer <b.helyer gmail.com> writes:
http://www.digitalmars.com/d/2.0/declaration.html

"In a declaration declaring multiple symbols, all the declarations must 
be of the same type:"

Yet this compiles:

---
    void main()
    {
        immutable a = 3, b = 4.2, c = true;
    }
---

a, b, and c all have different types. Unless you consider the type as 
'type to be inferred'. Can anyone explain this behaviour to me?
Jul 01 2010
next sibling parent reply dolive <dolive89 sina.com> writes:
Bernard Helyer Đ´µ˝:

 http://www.digitalmars.com/d/2.0/declaration.html
 
 "In a declaration declaring multiple symbols, all the declarations must 
 be of the same type:"
 
 Yet this compiles:
 
 ---
     void main()
     {
         immutable a = 3, b = 4.2, c = true;
     }
 ---
 
 a, b, and c all have different types. Unless you consider the type as 
 'type to be inferred'. Can anyone explain this behaviour to me?
 

immutable int a = 3, b = 4.2, c = true; // is error
Jul 01 2010
parent Bernard Helyer <b.helyer gmail.com> writes:
On Thu, 01 Jul 2010 06:26:34 -0400, dolive wrote:
  immutable int a = 3, b = 4.2, c = true;  // is error

Uhhh, yes. Yes it is. ?_?
Jul 01 2010
prev sibling next sibling parent reply FeepingCreature <default_357-line yahoo.de> writes:
On 01.07.2010 11:49, Bernard Helyer wrote:
 http://www.digitalmars.com/d/2.0/declaration.html
 
 "In a declaration declaring multiple symbols, all the declarations must 
 be of the same type:"
 
 Yet this compiles:
 
 ---
     void main()
     {
         immutable a = 3, b = 4.2, c = true;
     }
 ---
 
 a, b, and c all have different types. Unless you consider the type as 
 'type to be inferred'. Can anyone explain this behaviour to me?
 

Type deduction is a special case. I think the above was written before we had it.
Jul 01 2010
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
FeepingCreature:
 Type deduction is a special case. I think the above was written before we had
it.

OK. Special cases in a language are often bad, and I don't think that syntax is significantly handy. So if you agree Bernard Helyer can put it in bugzilla, asking to remove this special case: void main() { immutable a1 = 3, b1 = "hello"; auto a2 = 3, b2 = "hello"; static a3 = 3, b3 = "hello"; const a4 = 3, b4 = "hello"; } Bye, bearophile
Jul 01 2010
prev sibling next sibling parent Adam Ruppe <destructionator gmail.com> writes:
On 7/1/10, Adam Ruppe <destructionator gmail.com> wrote:
 (and auto, which is inferred from the missing type).

Nitpicking myself, but I shouldn't have said that; auto is just the default storage class, and it is the missing type that means infer it, not the auto keyword. I think you know what I mean though.
Jul 01 2010
prev sibling parent Adam Ruppe <destructionator gmail.com> writes:
I don't think this is really a special case: they are all sharing the
same keywords.

immutable a = 1, b = "a"; // both a and b are immutable
string a = "a", b = "b"; // both a and b are strings
immutable int a = 1, b = 2; // both a and b are immutable ints

I'd say the bug is that the documentation uses the wrong word; it
isn't "the same type" but rather that all must match the... stuff...
on the left, so the comma separated list is identifiers and
initializers, not types and storage classes (I don't know how to word
it best, but it makes perfect sense.)

What it forbids is C's habit of surprising you:

int* a, b; // surprise, b is of type int!

But, there's nothing really surprising in the immutable case. All
variables are indeed immutable (and auto, which is inferred from the
missing type).
Jul 01 2010
prev sibling parent canalpay <canalpayciftci gmail.com> writes:
I think :

Ýmmutable, type automatic moments. Because, it is StorageClass.

Example :
import std.stdio;
void main()
{
    immutable a = 3, b = 4.2, c = true;
    writeln(typeof(a).stringof,typeof(b).stringof,typeof(c).stringof);
}

or 

import std.stdio;
void main()
{
    auto a = 3, b = 4.2, c = true;
    writeln(typeof(a).stringof,typeof(b).stringof,typeof(c).stringof);
}

Keywords(StorageClass):
        abstract
        auto
        const
        deprecated
        extern
        final
        immutable
        shared
        nothrow
        override
        pure
        scope
        static
        synchronized

I don't know English. I'm sorry if misspelled.
Jul 01 2010