[Python-ideas] Modern language design survey for "assign and compare" statements
Steven D'Aprano
steve at pearwood.info
Sat May 19 09:41:07 EDT 2018
Thanks for writing up the summary, but you have picked a very narrow
subset of new languages. One might even say a biased subset. How about
these new languages?
Elixir, Elm, TypeScript, Hack, Julia, Perl6, Ring, LiveScript,
Ballerina, Crystal, Opa, Red, Ceylon
TL;DR
At least 10 out of these 13 include some form of assignment
expressions. If we include the five you selected, 13 out of 18, or
72% of the sample, include some form of assignment expression.
Details follow below.
Even in the limited set of five languages you chose, 3 out of the 5
allow some form of assignment expressions:
- Go allows assignment expressions in "if" statements, but not "while";
- Rust allows assignment expressions in both "if" and "while";
- as does Swift;
- Swift also allows assignment in arbitrary expressions, but it returns
Void (equivalent to returning None).
As for the other languages I found:
Elixir (2011):
- everything is an expression, including assignment
if current_user = Plug.Conn.get_session(conn, :current_user) do
Logger.info "User #{current_user.id} logged out"
end
https://stackoverflow.com/questions/42682961/assign-variable-and-test-in-if-statement-possible
Elm (2012)
- has "let expressions"
http://elm-lang.org/docs/syntax#let-expressions
TypeScript (2012)
- a strict superset of Javascript, including assignment expressions
Hack (2014)
- a (not quite full) superset of PHP, including assignment expressions
Julia (2012)
- includes assignment expressions
julia> x = 1
1
julia> y = (x = 2) + 1
3
julia> x
2
Perl 6 a.k.a. Rakudo (2015)
- assignment is an expression:
steve at orac ~ $ perl6 -e "say (my \$x = 32)+100; say \$x"
132
32
(the backslashes are escaping the dollar signs from the shell, they're
not part of the Perl syntax)
- also allows any(?) statement to be used as an expression
with the `do` prefix
Ring (2016)
Documentation seems fairly poor, more concerned with singing the praises
of the language than explaining the behaviour in detail, but maybe
that's just me. But I *think* assignment is not an expression. Using the
Ring interpreter here:
http://ring-lang.net/
I tried running this:
x = 55
x = (x=99)
see x
and got the output 0. My interpretation of this is that the x=99
expression is being interpreted as equals, not assignment.
LiveScript (2011)
- assignment is an expression which returns the value assigned
http://livescript.net/#assignment
Ballerina (2017)
- as far as I can tell, assignment is purely a statement
https://ballerina.io/res/Ballerina-Language-Specification-WD-2015-05-01.pdf
Crystal (2014)
- assignment is an expression which returns the value assigned
I don't know if this link will work:
https://play.crystal-lang.org/#/r/43fq
but if not, try running this:
x = 99;
y = (x = 33) + 1;
print x;
print y;
and the output ought to be 3334.
Opa (2011)
- assignment is an expression; the following two declarations
are equivalent:
two = {
one = 1 // semicolon and newline are equivalent
one + one
}
two = {
one = 1; one + one // the exact same thing as above
}
https://github.com/MLstate/opalang/wiki/The-core-language
Red (2011)
(not to be confused with the US DOD "RED" language from 1979).
I can't get the documentation to Red to display in a readable form in my
browser, but it claims to be nearly identical to Rebol.
Wikipedia describes Rebol (and presumably Red) as not having either
expressions or statements in usual sense. Based on my reading, it is
kinda-sorta like Forth except without the stack or the postfix syntax.
The creator of Red describes it:
"About the syntax and semantics, in a nutshell, it's a Lisp without
parentheses and with infix operator support."
which suggests that assignment could be an expression. There's also a
"set" function which can assign values, but frankly the Rebol
programming model confuses me and so I'll count this as a "Possibly, but
I can't tell for sure so let's say No" for the question of assignment
expressions.
Ceylon (2011)
- Ceylon has "let" assignment expressions.
https://ceylon-lang.org/documentation/1.3/tour/attributes-control-structures/#tip_let_expressions
- while loops support assignment
https://ceylon-lang.org/documentation/1.3/tour/attributes-control-structures/#while_loops
- and switch statements also support a named assignment
https://ceylon-lang.org/documentation/1.3/tour/attributes-control-structures/#switch_conditionals
--
Steve
More information about the Python-ideas
mailing list