Domanda

OK, so here's my code :

//================================================
// Constants
//================================================

const string YAML_STRING    =   "tag:yaml.org,2002:str";
const string YAML_INT       =   "tag:yaml.org,2002:int";
const string YAML_FLOAT     =   "tag:yaml.org,2002:float";
const string YAML_BOOL      =   "tag:yaml.org,2002:bool";

const string YAML_SEQ       =   "tag:yaml.org,2002:seq";
const string YAML_SET       =   "tag:yaml.org,2002:set";

const string YAML_MAP       =   "tag:yaml.org,2002:map";
const string YAML_OMAP      =   "tag:yaml.org,2002:omap";
const string YAML_PAIRS     =   "tag:yaml.org,2002:pairs";

//================================================
// Functions
//================================================

Value parseYAMLNode(Node n)
{
    writeln(n.tag);

    switch (n.tag)
    {
        case YAML_STRING    :   return new Value(n.as!(string));
        case YAML_INT       :   return new Value(n.as!(long));
        case YAML_FLOAT     :   return new Value(n.as!(float));
        case YAML_BOOL      :   return new Value(n.as!(bool));
        default             :
        }

        // more code - omitted
}

Once I decided to declare my case strings as consts (they are re-used, so I thought that'd be practical), it triggers a Case must be a string or an integral constant error.

Why is that? How can this be fixed?

È stato utile?

Soluzione

OK, so here's what I came up with...

if constants are declared like :

enum YAML_STRING = "...";

instead of const YAML_STRING = "...";

it works fine.


P.S. It still strikes me as a bit odd though...

Altri suggerimenti

From dlang:

Enum declarations are used to define a group of constants.

or, from Çehreli tutorial:

enum is the feature that enables defining named constant values.

instead const is a "type qualifier", and indicates a variables that cannot be modified.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top