Struct - static initialization "on-the-fly"

Marek Janukowicz marek at janukowicz.net
Tue Aug 20 16:33:57 PDT 2013


Ali Çehreli wrote:

> 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.

Too bad :( Is there any specific reason for that or is that just not 
implemented (yet)?

> 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

Yes, this fulfills my "single line" requirement, thanks!

-- 
Marek Janukowicz


More information about the Digitalmars-d-learn mailing list