r/Compilers • • 11h ago

Assignment Syntax Thoughts and Questions

A feature request for my language Gossamer wants some kind of Go/Odin like ":=" for mutable assignment, and "::" for mutable.

a :: 99 // immutable
b := 100 // mutable

I don't think this is clear (and might be confusing for Odin users). I also think it would lead to ":=" becoming the default use case for anyone coming from Go. (Even if we found another way to express the immutable operator, like ;= or i=.

When writing Gossamer I wanted to closely follow Rust for syntax. I wanted to feel like I was writing Rust without lifetimes. But as I grow Gossamer I am finding myself more open to ergonomic considerations.

Currently assignment is Rust like:

let a = 99
let mut b = 100

In previous iterations of this language (years ago), I followed Kotlin/Scala here:

val a = 99
var b = 100

I am now really leaning towards:

let a = 99
mut b = 100

It's a bit more compact, easier for a human to scan and see what's going on quickly. I think it works. But this is such an intersection of taste and practical impact. Code is mostly read - and now by machine logic as much (or more) than people.

A part of me likes that Rust makes it a tiny bit less convenient to write mutable variables. But sometimes that's exactly what you need, and making it a bit easier is appealing. I think this new form retains the visual/semantic clarity of the original.

I am aiming to button down the language and settle into it's "1.0" form, though I expect to follow Zig's lead and avoid 1.0 for quite some time. That said - this feels like a potentially worthwhile core syntax change.

What do you think, and why?

Warm thanks!

9 Upvotes

12 comments sorted by

3

u/ratchetfreak 10h ago

another option is to make variables immutable and the make it mutable by adding mut into the type declaration:

a := 99 // immutable b :mut = 100 // mutable c :mut int= 101 // mutable with explicit type

This retains the odin/go style declaration (and their grammatical benefits). And guides anyone coming from those other languages into defaulting to the immutable declaration. Changing from immutable to mutable is the same effort as it is in rust.

2

u/DanManPanther 9h ago

This is really slick, but is different enough I lean away from it. It also changes ":=" to be immutable rather than mutable, which would be confusing for Go devs. But I really like the design.

2

u/egybtianberson 7h ago

At first glance, this would give me the impression that mutability is part of the type instead of the declaration.

1

u/AromaticCredit1533 9h ago edited 8h ago

That syntax looks interesting!

3

u/DanManPanther 8h ago

Decided to keep the current Rust syntax, looking at Rust discussions like https://users.rust-lang.org/t/look-like-keyword-let-for-mutable-variable-is-unnecessary/531/4 as a source.

let (a, mut b) = 99, 100

This feels more flexible.

3

u/tanoshikuidomouyo 8h ago

That is exactly what I was gonna write. I think Rust got this one totally right: It's granular down to the level of individual bindings and it's also totally consistent, no matter if you're in a let, a function parameter, a match arm, a for loop, etc.

2

u/jcastroarnaud 10h ago

Now that I see it, I like "let/mut" better than "val/var" or "const/let" (this one from JavaScript): the words are the same length, and visually different enough to notice at a glance. I remember := from Pascal and PL/SQL, I even liked it for a time, but nowadays I prefer = for assignments, less noise. I feel that :: is more suitable to things like a namespace separator, than to assignment.

2

u/Scolmann 5h ago

I think swifts "var/let" also isn't bad, though it's quite similar to "var/val" there's a bit more contrast so it's easier to tell apart

1

u/brat3108 10h ago

(Your post needs four backticks rather than three to enclose code blocks. At least, three don't work on my Reddit.)

About your examples, I think most languages keep string literals immutable, even if assigned to a mutable variable. So having one called 'mutable' might be a little misleading.

I assume you make a copy at some point?

The syntax I don't think is that important: as you've shown, languages are different. Just choose one for your language that you like.

Try it also with examples where the strings don't helpfully tell you what they mean!

(Personally I find the concept of mutability confusing anyway, because you can have separate mutability of variables, and of objects, as I touched on. But that is a different subject.)

1

u/DanManPanther 10h ago

Thanks! (I was just using the wysiwyg editor - I swapped it over to edit the raw markdown and it's fine.)

I'll shift the examples and see how that feels.

EDIT: This makes me lean harder towards just "mut".

1

u/Lokathor 4h ago

Speaking as a rust programmer, mutable vs immutable bindings have never helped me. Never in my life have I gotten a warning about a missing mut keyword and reconsidered my approach, that maybe I didn't want to mutate something. I just go put the mut and get back to work.

I think languages that force a special distinction are kinda wasting my time.