New keywords `then` and `else` to improve readability (1)
Currently
andandormust be used with both conditions and actions, resulting in code that reads somewhat strangely:X == 1 or Y == 2 or False() => ZI propose to separate the
elsekeyword fromelse error onand allow it to be used on its own. It would behave the same asorbut be right-associative, allowing the above code to be written as:(X == 1 or Y == 2) else False() => ZThe
error onconstruct would remain on its own, and can still be used in the same situations:(X == 1 or Y == 2) else error "Error!" on ZOr in new situations:
e@X() :- where error "Error!" on eI also propose to introduce a new keyword
thenwhich would be the complement toelse. It would behave likeandbut be right-associative. The following code:X == 1 and Y == 2 and True() => ZCan then be written as:
(X == 1 and Y == 2) then True() => ZApart from improving readability, an added benefit is that this allows a construct that looks like an
if-else:Submitted by D. Pelsmaeker on 19 June 2014 at 12:33(X == 1 and Y == 2) then True() => Z else False() => Z
Issue Log
Makes sense
Do you want to try adding these to TS yourself? https://github.com/metaborg/ts
If
thenandelseare indeed sugar forandandor, it should be easy to add to TS using just some desugaring transformations.
I’ve attempted it, but it’s not really possible currently as all tasks are eagerly evaluated.
A and Bshould not evaluateBifAfails, but currently it does. Equivalently,A or Bshould not evaluate taskBifAdoesn’t fail.Therefore, if I split
elsefromerror on, then theerror ontask will always be evaluated and always display an error. (I could keep theelse error onbut thenelsedoesn’t add any benefits.)
(I know this is a year old, but just FYI)
If the error on is the only reason you need lazy evaluation, you can work around it, though you won’t be able to do it with just desugaring.
DefineA then Basnot(A) or (A and B), andA else BasA or BwhereBdepends onnot(A). That last part cannot be defined in sugar I guess.
Log in to post comments