[m-users.] if/then/else griping.
Zoltan Somogyi
zoltan.somogyi at runbox.com
Tue Jul 23 09:11:48 AEST 2019
On Mon, 22 Jul 2019 20:35:58 +1000, Peter Wang <novalazy at gmail.com> wrote:
> > *Syntax* issues with languages, I'd normally disregard as trivial,
> > but I don't think *formatting* is trivial.
Formatting is definitely not trivial, but it is also not something you can expect
everyone to agree on. While it is not *wholly* a matter of taste (some layout
styles are objectively worse than alternatives), it is nevertheless *partly*
a matter of taste, and arguing about taste is just about never useful:
de gustibus non disputandum.
> An automatic formatter would be nice for Mercury as well.
The biggest problem with automatic formatters is that they work well
*only* if they know exactly what syntactic construct each comment
is intended to be attached to. Is it the previous construct, or the next one?
And *which* previous or next construct? The last argument, or he last call?
The next call, or the next block of calls, or all of the following code in the
current predicate? While that is something that a formatter may *guess*,
it is not something it can *know*. And the more that a piece of code
is in need of reformatting, the less likely it is that the guess will be correct :-(
> However, the if/then/else keywords do have
> the advantage that when you are in the middle of some huge predicate
> (it happens) you don't need to go scanning very far forwards or
> backwards to find the telltale "->" that lets you know you are looking
> at an if-then-else rather than a conjunction or disjunction.
That is the main reason why I eventually came down on the side of if-then-else
syntax over (C -> T ; E) syntax. The minor reason is the fact that the compiler's
error messages for bad code such as (C -> T ; E ; F; G) were confusing ...
mixing if-then-elses and disjunctions is easier of their syntaxes are separate
and cannot be confused for each other.
That said, I think the best way to write code like the humpday example is this:
(
D = wednesday,
R = "humpday"
;
( D = monday
; D = tuesday
; D = thursday
; D = friday
),
R = "weekday"
;
( D = saturday
; D = sunday
),
R = "weekend"
)
We used to use if-then-elses for situations like this, but we rewrote most (maybe all)
of them in this style after I modified switch detection to handle switch arms that correspond
not to one but several function symbols. Unlike the if-then-else style, this style leads to
errors from the compiler if the type of the switched-on variable gets new function symbols,
which makes program maintenance significantly easier.
Zoltan.
More information about the users
mailing list