15 Jan, 2013, Runter wrote in the 1st comment:
Votes: 0
Does anyone know of a programming language that does this:

2d_map = {some_key: {second_key: "hello world"} }

if (2d_map["some_key"]["second_key"])
print it


In this example, it is a keyword that represents the value of the if statement. Anyone know a language do this without requiring setting a variable yourself?
15 Jan, 2013, Davion wrote in the 2nd comment:
Votes: 0
New to me.
15 Jan, 2013, Twisol wrote in the 3rd comment:
Votes: 0
Eh. An implicit binding is usually a bad sign, IMO. I like this version in Haskell, though.

ifMaybe :: Maybe a -> (a -> IO ()) -> IO ()
ifMaybe = flip $ maybe (return ())

2d_map = [('some_key', [('second_key', "hello world")])]

main = do
let item = (lookup "second_key" <=< lookup "some_key") $ 2d_map
ifMaybe item $ \it -> do
print it

– could shorten lines 6 and 7 to this, since it is such a short block:
– ifMaybe item print


Plus, you can chain arbitrarily deep into your object without getting an exception. If a Nothing is returned at any point, the whole lookup just resolves to Nothing.
15 Jan, 2013, Rarva.Riendf wrote in the 4th comment:
Votes: 0
Looks like 2 imbricated Java Hashmap to me. Or do I miss something.

No idea if this could compile though

public HashMap<String, HashMap <String, String>> myMap =  new HashMap() { put("firstkey", new HashMap() { put("secondKey", "test"}};
15 Jan, 2013, Davion wrote in the 5th comment:
Votes: 0
Rarva.Riendf said:
Looks like 2 imbricated Java Hashmap to me. Or do I miss something.

Implicit declaration of the 'it' keyword based on the evaluation of the if statement.
15 Jan, 2013, Twisol wrote in the 6th comment:
Votes: 0
You just taught me a new word! But I think he was asking about having a variable automatically and implicitly bound to the result of the 'if' condition.
15 Jan, 2013, Rarva.Riendf wrote in the 7th comment:
Votes: 0
Davion said:
Implicit declaration of the 'it' keyword based on the evaluation of the if statement.


I knew I missed something somehow…
15 Jan, 2013, Tyche wrote in the 8th comment:
Votes: 0
Runter said:
In this example, it is a keyword that represents the value of the if statement. Anyone know a language do this without requiring setting a variable yourself?

I don't see why a language couldn't. I don't know of any, but depending on block scoping rules of a particular language, it might be highly undesirable in some.
15 Jan, 2013, Runter wrote in the 9th comment:
Votes: 0
Okay, I'll admit it. I'm being super lazy.

I think I was confusing in my original post so this is what I mean:

if exp
print it


being the same as:
var = exp

if exp
print var


Yes, very lazy. I just see the pattern come up a lot so I was curious if any language did something like this. I couldn't think of any.
15 Jan, 2013, Rarva.Riendf wrote in the 10th comment:
Votes: 0
Well if you are that lazy, factorize!

void ifprint(void toto) {
if (toto) print toto
}

ifprint(toto)

in the long run, there is even fewer character than "if toto print toto"
15 Jan, 2013, Kaz wrote in the 11th comment:
Votes: 0
I don't know of a language that does this. The problem I see is that it breaks down far too easily with anything but the most trivial logic.

Consider:

if (a = b and c = d)
print it


What gets printed?
15 Jan, 2013, Runter wrote in the 12th comment:
Votes: 0
Kaz said:
I don't know of a language that does this. The problem I see is that it breaks down far too easily with anything but the most trivial logic.

Consider:

if (a = b and c = d)
print it


What gets printed?


It depends on the language in question, but whatever the expression evaluates to. In some languages that will evaluate to true/false. In all languages I'm aware of if statements evaluate to a single value.
15 Jan, 2013, Idealiad wrote in the 13th comment:
Votes: 0
I guess it depends if you actually want to keep the var around. In a language with s-expressions for example this is the only way it works.

> (println (if 1 "foo"))
foo
nil
>


Clojure has an if-let:

> (if-let [it 1] (println it))
1
nil
>


But if-let is just a macro, so pretty much what RR was talking about.
17 Jan, 2013, Scandum wrote in the 14th comment:
Votes: 0
TinTin++ supports this behavior for regular expression if checks. One problem in the given example is how to handle:

if (bla)
if (bli)
print it


A language could support %if, %%if for the 2nd nest, etc. It'd be pure bloat imo.
17 Jan, 2013, Runter wrote in the 15th comment:
Votes: 0
Scandum said:
TinTin++ supports this behavior for regular expression if checks. One problem in the given example is how to handle:

if (bla)
if (bli)
print it


I would expect in this case to print the value of bli.
19 Jan, 2013, Telgar wrote in the 16th comment:
Votes: 0
Doesn't perl do this?

if (bla)
print $_;
0.0/16