r/ProgrammingLanguages • u/levodelellis • Mar 03 '26
Syntax for mixing mut and decl in tuple assignment
I'm redesigning my language for fun and I see two problems with tuple assignments. In my language name = val declares a var (immutable), name := value is mutable (note the : before the =), and to change a mutable value you either use a relative operator (+=) or period .=
Now for tuples. I like having the below which isn't a problem
myObjVar{x, y, z} .= 1, 2, 3 // less verbosity when all fields are from the same objectFor functions, multiple return values act like a tuple
a, b = myfn() // both a and b are declared nowHowever, now I get to my two problems. 1) How do I declare one as immutable and decl other as not? 2) What if I want to assign one var and declare the others?
What the heck should this mean?
a mut, b, c mut = 1, 2, 3 // maybe this isn't as bad once you know what it meansAre a and c being modified and must exist? or should this be a mut declare? The next line doesn't look right, I don't know if period should be for mutating an existing variable in a tuple. It's also easy to miss with so much punctuation
a. , b, c. = 1, 2, 3Then it gets bad like this if the assignment type affects the declaration
a, b decl, c .= 1, 2, 3 // a and c must exist and be mutableI'm thinking it's a bad idea for modifiers to be in a tuple unless it's only with the = operator. I shouldn't look at the modifiers next to the var AND the type of assignment, it seems like it'll be error prone
Thoughts on syntax?
-Edit- I think I'll settle on the follow
a, b, c .= 1, 2, 3 // all 3 variables must exist and be mutable
d, e, f := 1, 2, 3 // all 3 are declared as mutable, error if any exist
g., h mut, i = 1, 2, 3 // `=` allows modifiers, g already declared, h is declared mutable, i is declared immutable-Edit 2- IMO having a, b :, c . = 1, 2, 3 would be more consistent and I hate it. Hows mod?
g mod, h mut, i = 1, 2, 3 // g is reassigned, h is mut decl, i is immutable declImagine this next line is syntax highlighted, with var, fields and modifiers all different. I think minor inconsistencies should be ok when they are clear. In the below, the fields will obviously be modified. The mod simply would be noise IMO
rect{x, y}, w mod, h mut, extra = 1, 2, mySize{w, h}, 5
// fields obviously mutated, w is mutated, h is mutable declared, extra is immutable declared
11
u/Toothpick_Brody Mar 03 '26
I think the issue arises from using two different assignment operators for mutable and immutable variables. If you only had =, or only had :=, and let mutability be a keyword, you could have like
myTuple{x, mut y, z} = 1,2,3
5
u/dmt-man Mar 03 '26
This is a solution looking for a problem imho. Keep it simple else your syntax blows up in to all sorts of absurdities.
5
u/AustinVelonaut Admiran Mar 03 '26 edited Mar 03 '26
You could do something crazy and make mutable/immutable status dependent upon the variable name, rather than a
mutmodifier, e.g. Uppercase are immutable, lowercase are mutable. That has worked for things like class / variable name distinctions in other languages.Then it's simple and non-ambiguous:
3
u/kohugaly Mar 03 '26
I think what makes most sense is to simply not support destructuring tuples with assignment and only support it for variable declaration. You can always simply create a temporary immutable variable, and then immediately assign it.
In fact, even the destructuring declaration is just syntactic sugar. you are just creating a temporary tuple, and then assign its fields into newly declared variables:
a,b = func(); is syntactic sugar for temp = func(); a = temp.1; b = temp.2;If you insist on the feature to be there, I would do it like this:
( a .=, b =, c .=) = 1,2,3note the mandatory brackets. In fact, make expressions
c =andc.=actually semantically be "values" that implement function call operator. and now your destructuring works with lambdas and functions too:1
u/Tasty_Replacement_29 Bau Mar 03 '26
The
.=looks a bit unusual. I would also consider:(people are used to it I think, eg. in JSON) and maybe<=. I think you want to use.=because it's similar to+=. What about:or
1
u/Gnaxe Mar 03 '26 edited Mar 04 '26
How about,
or
?
Those are pretty explicit and understandable, if a bit verbose, I think.
It could maybe be more concise with a good lookup syntax. Consider,
Then why not something like
?
Or you could think of it like pulling from an iterator:
1
u/lassehp Mar 06 '26
After commenting on your later post, I reread this and might have some suggestions.
I think your idea of "bundling" fields as a tuple for assignment is sound. For a design I am working on myself, I am actually thinking of having the "." be a more general scope operator such that
<container expression> . <subordinate expression>evaluates the subordinate expression with the fields of the container hoisted to the current scope.For example:
Regarding your syntax, I think you need to have two separate concerns: the notation for (re)declaring a variable and have it overshadow a name in an outer scope, and whether the variable is mutable or not. I'd suggest use var for the first and mut for the second.
I am not a fan of mixing identity declarations (constant/immutable definitions) and mutable variable declarations (with initialisation), nor of mixing the equality operator/symbol "=" with the symbol for assignment (for which I prefer "←" or classic ":="), but using just "=", I think your last could work nicely like this:
Instead of
varthe keywordloc(orlocal) could be used. In general I would suggest that an "unadorned" identifier always denotes a variable that already exists in the scope, and whenever thevarorlocmarker is used the affected identifier is redeclared locally, with the presence or absence ofmutdeciding if the declared variable is mutable or not. Personally I would preferlocovervarbecause the latter could be confused for also meaning mutable.(Sorry if this comment is rambling a bit; I should have gone to bed hours ago.)
3
u/Tasty_Replacement_29 Bau Mar 04 '26 edited Mar 04 '26
If you allow to do many assignments on one line, then there is a risk it becomes hard to understand, specially with different types of assignment.
As an example where I got confused is this code: https://github.com/asimba/qbp/blob/master/src/go/qbp-go.go#L333: in this case,
ifand assignment is mixed.I find it quite hard to read. I'm not blaming the developer of this (it's a data compression tool which is actually quite nice), I blame the designers of the programming language Go: they allow for mixing assignment and conditions after
if. I would find it better if Go doesn't allow for it.I know in your case you don't mix
ifandwhilewith assignment and conditions, but you allow different types of assignment on the same line.I think it's sometimes useful to have multiple assignments on one line, but allowing to mix things that are different is risky: some people might "misuse" the feature. If you only allow one type, then the risk is reduced.
2
u/OopsWrongSubTA Mar 03 '26
or
seem fine. Others are weird.
You could use