On 03.11.2024 18:00, David Brown wrote:
On 02/11/2024 21:44, Bart wrote:
>
(Note that the '|' is my example is not 'or'; it means 'then':
>
( c | a ) # these are exactly equivalent
if c then a fi
>
( c | a | ) # so are these
if c then a else b fi
>
There is no restriction on what a and b are, statements or
expressions, unless the whole returns some value.)
Ah, so your language has a disastrous choice of syntax here so that
sometimes "a | b" means "or", and sometimes it means "then" or
"implies", and sometimes it means "else".
(I can't comment on the "other use" of the same syntax in the
"poster's language" since it's not quoted here.)
But it's not uncommon in programming languages that operators
are context specific, and may mean different things depending
on context.
You are saying "disastrous choice of syntax". - Wow! Hard stuff.
I suggest to cool down before continuing reading further. :-)
Incidentally above syntax is what Algol 68 supports; you have
the choice to write conditionals with 'if' or with parenthesis.
As opposed to "C", where you have also *two* conditionals, one
for statements (if-then-else) and one for expressions ( ? : ),
in Algol 68 you can use both forms (sort of) "anywhere", e.g.
IF a THEN b ELSE c FI
x := IF a THEN b ELSE c FI
IF a THEN b ELSE c FI := x
or using the respective alternative forms with ( a | b | c) ,
or ( a | b ) where no 'ELSE' is required. (And there's also
the 'ELIF' and the '|:' as alternative form available.)
BTW, the same symbols can also be used as an alternative form
of the 'case' statement; the semantic distinction is made by
context, e.g. the types involved in the construct.
I can understand if this sounds strange and feels unfamiliar.
Why have a second syntax with
a confusing choice of operators when you have a perfectly good "if /
then / else" syntax?
Because, depending on the program context, that may not be as
legible as the other, simpler construct.
Personally I use both forms depending on application context.
In some cases one syntax is better legible, in other cases the
other one.[*]
In complex expressions it may even be worthwhile to mix(!) both
forms; use 'if' on outer levels and parenthesis on inner levels.
(Try an example and see before too quickly dismiss the thought.)
Or if you feel an operator adds a lot to the
language here, why not choose one that would make sense to people, such
as "=>" - the common mathematical symbol for "implies".
This is as opinion of course arguable. It's certainly also
influenced where one is coming from (i.e. personal expertise
from other languages). The detail of what symbols are used is
not that important to me, if it fits to the overall language
design.
From the high-level languages I used in my life I was almost
always "missing" something with conditional expressions. I
don't want separate and restricted syntaxes (plural!) in "C"
(for statements and expressions respectively), for example.
Some are lacking conditional expressions completely. Others
support the syntax with a 'fi' end-terminator and simplify
structures (and add to maintainability) supporting 'else-if'.
And few allow 'if' expressions on the left-hand side of an
assignment. (Algol 68 happens to support everything I need.
Unfortunately it's a language I never used professionally.)
I'm positive that folks who use languages that support those
syntactic forms wouldn't like to miss them. (Me for sure.)
("disastrous syntax" - I'm still laughing... :-)
Bart, out of interest; have you invented that syntax for your
language yourself of borrowed it from another language (like
Algol 68)?
Janis
[*] BTW, in Unix shell I also use the '||' and '&&' syntax
shortcuts occasionally, in addition to the if/then/else/fi
constructs, depending on the application context.