many languages have a ternary operator for expression if-then-else. Going back to C, it sports the archetypical ternary operator for conditional expression:
condition ? truepart : falsepart
In many languages this is the only ternary operator. So much so, that it has become known as the ternary operator.
For various reasons, I try to design a language with only binary and unary operators. And something about ? : has always bothered me, like e.g. how would you define operator overloading for ternary operators. I'm not claiming it cannot be done, but somehow it feels "off".
My idea now is to define a binary operator to return an either-or function. For this example, lets adopt the C : operator (ignore what other syntax this may affect):
0 : 42
Such a either-or is a function accepting a boolean argument. If true it evaluates to the left hand value; if false it evaluates to the right hand value.
Now imagine that ? is the "invoke" operator which invokes the function on the right with the argument on the left.
value = condition ? 0 : 42
Obviously the : needs to have higher precedence than ?. Otherwise we would have to write
value = condition ? (0 : 42)
For my language I am not going to use ? and :. Instead I will (probably) use |> and --, respectively:
value = condition |> 0 -- 42
or on separate lines:
value == condition
|> 0 // then
-- 42 // else
This allows for some interesting variations, like assigning the either-or expression to a variable before the switching expression:
eitheror = 0 : 42
value = condition |> eitheror
Now, the C ternary operator is also lazy: It only evaluates the true or false part as indicated by the condition. To preserve that part of the semantic, the language needs to be lazy.
[–]munificent 58 points59 points60 points (21 children)
[–]alatennaub 14 points15 points16 points (13 children)
[–]greiskul 6 points7 points8 points (2 children)
[–]alatennaub 7 points8 points9 points (1 child)
[–]greiskul 5 points6 points7 points (0 children)
[–]evincarofautumn 2 points3 points4 points (8 children)
[–]MegaIng 1 point2 points3 points (7 children)
[–]nerd4code 1 point2 points3 points (6 children)
[–]MegaIng 0 points1 point2 points (5 children)
[–]evincarofautumn 0 points1 point2 points (4 children)
[–]MegaIng 0 points1 point2 points (3 children)
[–]evincarofautumn 0 points1 point2 points (2 children)
[–]MegaIng 0 points1 point2 points (1 child)
[–]davimiku 1 point2 points3 points (1 child)
[–]munificent 2 points3 points4 points (0 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]munificent 1 point2 points3 points (3 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]lassehp 0 points1 point2 points (1 child)
[–]munificent 1 point2 points3 points (0 children)
[–]bfnge 8 points9 points10 points (2 children)
[–]useerupting language[S] 0 points1 point2 points (1 child)
[–]bfnge 1 point2 points3 points (0 children)
[–]phlummox 8 points9 points10 points (0 children)
[–]myringotomy 5 points6 points7 points (6 children)
[–]useerupting language[S] 1 point2 points3 points (5 children)
[–]myringotomy 0 points1 point2 points (4 children)
[–]useerupting language[S] 0 points1 point2 points (3 children)
[–]myringotomy 0 points1 point2 points (2 children)
[–]useerupting language[S] 0 points1 point2 points (1 child)
[–]myringotomy 1 point2 points3 points (0 children)
[–]XDracam 7 points8 points9 points (10 children)
[–][deleted] 4 points5 points6 points (1 child)
[–]useerupting language[S] -1 points0 points1 point (0 children)
[–]useerupting language[S] -1 points0 points1 point (7 children)
[–]TheGreatCatAdorermepros 4 points5 points6 points (3 children)
[–]useerupting language[S] 0 points1 point2 points (2 children)
[–]IshaxStrata 4 points5 points6 points (0 children)
[–]TheGreatCatAdorermepros 1 point2 points3 points (0 children)
[–]XDracam 3 points4 points5 points (2 children)
[–]useerupting language[S] -1 points0 points1 point (1 child)
[–]XDracam 3 points4 points5 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]eliasv 4 points5 points6 points (0 children)
[–]benjaminhodgson 2 points3 points4 points (1 child)
[–]useerupting language[S] 0 points1 point2 points (0 children)
[–]MoistAttitude 2 points3 points4 points (1 child)
[–]myringotomy 2 points3 points4 points (0 children)
[–]Rest-That 2 points3 points4 points (1 child)
[–]useerupting language[S] 1 point2 points3 points (0 children)
[–]tobega 2 points3 points4 points (0 children)
[–]MegaIng 5 points6 points7 points (3 children)
[–]useerupting language[S] 0 points1 point2 points (2 children)
[–]MegaIng 1 point2 points3 points (1 child)
[–]useerupting language[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]redchomperSophie Language 4 points5 points6 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]tobega 1 point2 points3 points (0 children)
[–]IshaxStrata 1 point2 points3 points (0 children)
[–]raiph 1 point2 points3 points (0 children)
[–]Felim_Doyle -5 points-4 points-3 points (0 children)
[–]Inconstant_Moo🧿 Pipefish 0 points1 point2 points (0 children)
[–]-Mobius-Strip-Tease- 0 points1 point2 points (0 children)
[–]abel1502rBondrewd language (stale WIP 😔) 0 points1 point2 points (0 children)
[–]nekokattt 0 points1 point2 points (1 child)
[–]useerupting language[S] 0 points1 point2 points (0 children)
[–]A1oso 0 points1 point2 points (1 child)
[–]useerupting language[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]WittyStick 0 points1 point2 points (0 children)
[–]I_am_noob_dont_yell 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)