If I wrote a language...

It would use this really sexy syntax a friend turned me onto today, which according to him works in Perl and ActionScript:

This situation comes up often: Some function can return a value OR
null
. You then want to do an assignment with that value, but do something different if it had returned
null
. Instead of,
myVariable = some_Function_That_Might_Return_Null();
If (myVariable != null) {
     myValue = myVariable;
else
     myValue = some_Default_Value;
}
You have the very cute:
myVariable = some_Function_That_Might_Return_Null();
myValue = myVariable || some_Default_Value;
myValue gets set to myVariable if it's not
null
. If myVariable is
null
, it uses the second value.
In fact, I suspect that one could use (or could implement the use of) statements, not just variables:
myValue = some_Function_That_Might_Return_Null() || some_Default_Value;

Of course, that looks similar to "?:" (sometimes called the ternary operator)

myValue = (myBooleanFunction()) ? "Yay!" : "Boo";

I suspect that using the
or 
operator this way might be a side effect of the implementation of
null
-like values or the implementation of Boolean conditional evaluation. Possibly with some help from the use of short circuiting.

Additionally
Methods that return a value would be declared with "fun". Because the larger your project, the more fun it would have!

0 things about

If I wrote a language...

Post a Comment

Copyright 2012 Phile not Found. See About
Powered by Blogger

"Whenever you find that you are on the side of the majority, it is time to pause and reflect."