www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Struct - static initialization "on-the-fly"

reply Marek Janukowicz <marek janukowicz.net> writes:
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.

-- 
Marek Janukowicz
Aug 20 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Aug 20 2013
parent Marek Janukowicz <marek janukowicz.net> writes:
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
Aug 20 2013