www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - switch(string)

reply David Ferenczi <raggae ferenczi.net> writes:
The following code gives the following compilation error:

Error: case must be a string or an integral constant, not BAR1
Error: case must be a string or an integral constant, not BAR2

--------------------------8<------------------------------------
int main(string[] args)
{
    string BAR1 = "bar1";
    string BAR2 = "bar2";
    string myString = "bar3";

    switch (myString)
    {
        case BAR1:
            break;

        case BAR2:
            break;

        defualt:
            break;
    }

    return 0;
}
--------------------------8<------------------------------------

Can somebody explain me why?

Thanks,
David
Jun 18 2008
parent reply Robert Fraser <fraserofthenight gmail.com> writes:
David Ferenczi wrote:
 The following code gives the following compilation error:
 
 Error: case must be a string or an integral constant, not BAR1
 Error: case must be a string or an integral constant, not BAR2
 
 --------------------------8<------------------------------------
 int main(string[] args)
 {
     string BAR1 = "bar1";
     string BAR2 = "bar2";
     string myString = "bar3";
 
     switch (myString)
     {
         case BAR1:
             break;
 
         case BAR2:
             break;
 
         defualt:
             break;
     }
 
     return 0;
 }
 --------------------------8<------------------------------------
 
 Can somebody explain me why?
 
 Thanks,
 David

BAR1 and BAR2 are not constant. In D1, try: const BAR1 = "bar1"; In D2, you can also try: invariant BAR1 = "bar1"; enum BAR1 = "bar1"; The enum version and the const version in D1 will not take up any storage.
Jun 18 2008
parent reply David Ferenczi <raggae ferenczi.net> writes:
Robert Fraser wrote:

 David Ferenczi wrote:
 The following code gives the following compilation error:
 
 Error: case must be a string or an integral constant, not BAR1
 Error: case must be a string or an integral constant, not BAR2
 
 --------------------------8<------------------------------------
 int main(string[] args)
 {
     string BAR1 = "bar1";
     string BAR2 = "bar2";
     string myString = "bar3";
 
     switch (myString)
     {
         case BAR1:
             break;
 
         case BAR2:
             break;
 
         defualt:
             break;
     }
 
     return 0;
 }
 --------------------------8<------------------------------------
 
 Can somebody explain me why?
 
 Thanks,
 David

BAR1 and BAR2 are not constant. In D1, try: const BAR1 = "bar1"; In D2, you can also try: invariant BAR1 = "bar1"; enum BAR1 = "bar1"; The enum version and the const version in D1 will not take up any storage.

Thank you very much for the quick reply. Does it mean that a constant/invariant doesn't need an explicit storage type? I thought that string was invariant, so it was constant. Isn't it right? Thanks, David
Jun 18 2008
parent reply Robert Fraser <fraserofthenight gmail.com> writes:
David Ferenczi wrote:
 Robert Fraser wrote:
 
 David Ferenczi wrote:
 The following code gives the following compilation error:

 Error: case must be a string or an integral constant, not BAR1
 Error: case must be a string or an integral constant, not BAR2

 --------------------------8<------------------------------------
 int main(string[] args)
 {
     string BAR1 = "bar1";
     string BAR2 = "bar2";
     string myString = "bar3";

     switch (myString)
     {
         case BAR1:
             break;

         case BAR2:
             break;

         defualt:
             break;
     }

     return 0;
 }
 --------------------------8<------------------------------------

 Can somebody explain me why?

 Thanks,
 David

const BAR1 = "bar1"; In D2, you can also try: invariant BAR1 = "bar1"; enum BAR1 = "bar1"; The enum version and the const version in D1 will not take up any storage.

Thank you very much for the quick reply. Does it mean that a constant/invariant doesn't need an explicit storage type?

Yes, but you can write it as: invariant string BAR1 = "bar1"; If "string" isn't specified it will be deduced.
 I thought that string was invariant, so it was constant. Isn't it right?

string is invariant(char)[]. So the letters are constant, but not the reference. This means: string x = "abc"; x[2] ='d'; // ERROR x = "abd"; // Okay It's kind of confusing at first, and I have yet to be convinced of its usefulness, but there ya go.
Jun 18 2008
next sibling parent David Ferenczi <raggae ferenczi.net> writes:
Robert Fraser wrote:

 David Ferenczi wrote:
 Robert Fraser wrote:
 
 David Ferenczi wrote:
 The following code gives the following compilation error:

 Error: case must be a string or an integral constant, not BAR1
 Error: case must be a string or an integral constant, not BAR2

 --------------------------8<------------------------------------
 int main(string[] args)
 {
     string BAR1 = "bar1";
     string BAR2 = "bar2";
     string myString = "bar3";

     switch (myString)
     {
         case BAR1:
             break;

         case BAR2:
             break;

         defualt:
             break;
     }

     return 0;
 }
 --------------------------8<------------------------------------

 Can somebody explain me why?

 Thanks,
 David

const BAR1 = "bar1"; In D2, you can also try: invariant BAR1 = "bar1"; enum BAR1 = "bar1"; The enum version and the const version in D1 will not take up any storage.

Thank you very much for the quick reply. Does it mean that a constant/invariant doesn't need an explicit storage type?

Yes, but you can write it as: invariant string BAR1 = "bar1"; If "string" isn't specified it will be deduced.
 I thought that string was invariant, so it was constant. Isn't it right?

string is invariant(char)[]. So the letters are constant, but not the reference. This means: string x = "abc"; x[2] ='d'; // ERROR x = "abd"; // Okay It's kind of confusing at first, and I have yet to be convinced of its usefulness, but there ya go.

Thank you very much! :-) I'm on the way to clarity. ;-)
Jun 18 2008
prev sibling parent reply "Nick Sabalausky" <a a.a> writes:
"Robert Fraser" <fraserofthenight gmail.com> wrote in message 
news:g3al00$20ml$1 digitalmars.com...
 invariant string BAR1 = "bar1";

 If "string" isn't specified it will be deduced.

To clarify: invariant BAR1 = "bar1"; Is shorthand for: invariant auto BAR1 = "bar1"; And "auto" means the type is deduced.
Jun 18 2008
parent BCS <ao pathlink.com> writes:
Reply to Nick,

 "Robert Fraser" <fraserofthenight gmail.com> wrote in message
 news:g3al00$20ml$1 digitalmars.com...
 
 invariant string BAR1 = "bar1";
 
 If "string" isn't specified it will be deduced.
 

invariant BAR1 = "bar1"; Is shorthand for: invariant auto BAR1 = "bar1"; And "auto" means the type is deduced.

Almost (the difference is almost irrelevant, I think), auto is a do-nothing attribute that is used if no other attributes are used. The syntax for type deduction is "<attributes> <name> = <exp>"
Jun 18 2008