acton icon indicating copy to clipboard operation
acton copied to clipboard

Use separate assignment operators for constants and variables?

Open plajjan opened this issue 9 months ago • 2 comments

I saw a cool thing in Jai, assignments look like this:

a: int = 123

if something is a constant, = is replaced with :

b: int : 123

and these can be shortened to:

a := 123
b :: 123

pretty nice, I got to say.

And made me think, can we use : for constants? We currently require var for variable actor attributes and have talked about doing the same for other variables and changing the default to immutables everywhere. It was quite some time ago that we said immutable default is good. I want to like it, but still can't feel it's a huge divergence from our otherwise pythonic syntax. Is it better to use a different assignment operator for constants? Jai does this shortening that won't work in Acton perfectly in Acton since we already use = and not := and I don't really think that should change.

maybe??

myvar = 123
myconst := 456

plajjan avatar Apr 05 '25 05:04 plajjan

The obstacle here is that := means variable (re)assignment in pretty much every language that uses the operator. Introducing the opposite reading in Acton looks like it could lead to much confusion. And switching interpretations to

myconst = 123
myvar := 456

of course misses the goal of retaining the Pythonic flavor of local variable assignments.

I think the challenge we face is not so much about finding a friendly syntax for constant bindings as it is about supporting natural defaults. We already agree on the difference between variable and constant bindings, so writing

let myconst = 123
var myvar = 456

would allow us to spell that out clearly (with very little Pythonic flavor remaining!). But what if we also could combine this with some context-dependent defaults? What if

x = 42

could mean

var x = 42

whenever it occurs as a statement inside a function (and x is previously unbound), while

let x = 42

would be the implied reading on the top level and in class and actor attribute definitions? Isn't it in the latter contexts we have the strongest reasons for promoting immutability as the default?

As a consequence, the programmer would have to write

let local_const = 123

if a local constant is needed inside a function, and

var mutable_attribute = 456

if a mutable class attribute is desired (like it currently is in actors).

Not so bad, I think...

nordlander avatar Apr 07 '25 08:04 nordlander

@nordlander just to be clear, in Jai, := is variable (re)assignment, like many other languages. It is :: that defines a constant. My spontaneous suggestions wasn't particularly thought through, perhaps it would be confusing for Acton to use := for constants but I dunno, so many languages are using similar concepts with slight variation that it just seems hard to do anything if we are to be too conformant to what others are doing. I just thought that : felt constant-ish, foo : 123 might conflict with type declarations though, but maybe foo :: 123 could be an alternative.

I'm torn on context-dependent behavior... food for thought

plajjan avatar Apr 07 '25 08:04 plajjan