Struct - static initialization "on-the-fly"
Ali Çehreli
acehreli at yahoo.com
Tue Aug 20 16:14:24 PDT 2013
On 08/20/2013 03:47 PM, Marek Janukowicz wrote:> In this program:
>
> import std.stdio, std.json;
>
> void main () {
> JSONValue jsonSet;
> jsonSet.type = JSON_TYPE.OBJECT;
> JSONValue a = { type: JSON_TYPE.STRING, str: "abcde" }; // This work
> jsonSet.object["a"] = a;
> jsonSet.object["b"] = { type: JSON_TYPE.STRING, str: "jjf" }; //
THIS LINE
> }
>
> marked line will not compile failing with:
> json.d(8): Error: found ':' when expecting ';' following statement
>
> Is there any way to selectively initialize a struct while using it as AA
> value? The way I set "a" key works, but requires two lines instead of one
> and I have many such elements to set. I can't initialize all members of
> JSONValue, because there are many and I only need those two.
>
Unfortunately, that syntax works only when initializing a variable.
Would you like the following syntax instead?
root.object["a"] = to!JSONValue("abcde");
root.object["b"] = to!JSONValue("jjf");
If so, the following thread has a rough implementation of a 'to'
specialization for JSONValue:
http://forum.dlang.org/post/jl6bsn$67k$1@digitalmars.com
Ali
More information about the Digitalmars-d-learn
mailing list