D JSON (WAT?!)
    H. S. Teoh via Digitalmars-d-learn 
    digitalmars-d-learn at puremagic.com
       
    Thu Jul 24 09:00:32 PDT 2014
    
    
  
On Thu, Jul 24, 2014 at 03:54:20PM +0000, Pavel via Digitalmars-d-learn wrote:
[...]
> Guess what, here's a new snippet:
> 
> import std.stdio;
> import std.json;
> 
> void main() {
>   scope(failure) writeln("FaILED!!");
>   string jsonStr = `{ "name": "1", "type": "r" }`;
>   auto parsed = parseJSON(jsonStr).object;
>   writeln("fail" in parsed);
> }
> 
> Output is:
> null
> 
> WAT?!
> 
> Ofcourse, writing like:
> 
> writeln(cast(bool)("fail" in parsed));
> 
> Produces "false"... but why on earth boolean expression would output null?
It's not a boolean expression. The 'in' operator returns a pointer.
Rationale: avoid double lookups, for example:
	if (auto ptr = "key" in assocArray) {
		doSomething(*ptr);
	}
T
-- 
The right half of the brain controls the left half of the body. This means that only left-handed people are in their right mind. -- Manoj Srivastava
    
    
More information about the Digitalmars-d-learn
mailing list