Consider Ternary operator
Closed this issue · 7 comments
In the interest of C-family congruence.
Decided not to add ternary operator to KobraScript.
Re-opening. KS will have a ternary expression soon. Take the below code for example:
fn BinarySearchTree():
$ root
fn size():
return 0 if (!root) .. else root.size()
end
fn height():
return 0 if (!root) .. else root.height()
end
fn insert(data):
if (!root):
root = node(data)
.. else:
root.insert(data)
end
end
end
fn node(data):
$ data = data
$ lesser
$ greater
fn size():
$ sizeLeft = lesser.size() if (lesser) .. else 0
$ sizeRight = greater.size() if (greater) .. else 0
return 1 + sizeLeft + sizeRight
end
fn height():
$ heightLeft = lesser.height() if (lesser) .. else 0
$ heightRight = greater.height() if (greater) .. else 0
return 1 + heightLeft if (heightLeft > heightRight) .. else 0
end
fn insert(d):
if (d < data):
lesser.insert(d) if (lesser) .. else lesser = node(d)
.. else:
greater.insert(d) if (greater) .. else greater = node(d)
end
end
end
Re-assuming my position on ternary operators: statements should be evaluated one way or be evaluated functionally.
If there exists conditionality, that should be a statement. KobraScript does a good job with expressing short conditionals succinctly. Only-if statements, and Single-statement blocks help here.
So are you going to add a conditional expression or not? Going to take Go's lead and keep it out?
Going to keep it out. If it's important enough to be based on some boolean variable that is just floating in scope, that ought to be a statement as it is a clear fork in the flow of the program. KS is a bit nicer than go because simple conditionals can be expressed in fewer lines.
if (data.length > 0)
-> return parseDataArray(data)
else
-> return parseDataObject(data)
I am open to better solutions for strings (over basic +
concatenation.) Maybe python's %
?
String interpolation would be fire in KobraScript. Really is preferable in a lot of ways.
Edit: How Ruby does string interpolation -> #{expression}
Why do you say your conditionals can be expressed in fewer lines?
Do you have both
if exp
block
else
block
end
and
if (exp) -> statement else -> statement
with the second form not having a block? The mixture of statements, blocks, and expressions confuses me.
One language that doesn't have a conditional expression is Lua. They just ask you to write things like
x and y or z
so if x is true you evaluate y otherwise where x is false there's no neeed to evaluate y because the conjuction of x and y is false, so you have to go evaluate z. This is the same as "if x then y else z" except when y is false, which is rare in practice.
If KS can do statements as expressions you can adopt that as your style. Or really, why not just do conditionals? So many ways to write them:
return data > 0 ? parseDataArray() : parseDataObject()
return parseDataObject() if data > 0 else parseDataArray()
return data > 0 -> parseDataArray() | parseDataObject()
return if (data > 0) parseDataArray() | parseDataObject()
or even the multiline
return
data > 0 -> parseDataArray
true -> parseDataObject
I don't understand your current version without the end
keyword which is used everywhere else.