Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add special syntax for mutable variable declarations #1273

Closed
boggle opened this issue Dec 8, 2011 · 24 comments
Closed

add special syntax for mutable variable declarations #1273

boggle opened this issue Dec 8, 2011 · 24 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@boggle
Copy link
Contributor

boggle commented Dec 8, 2011

we just discussed this on irc and everybody on seemed to like this idea:

let x = 7 // declaration of immutable variable x
mut y = 7 // declaration of mutable variable y

This ticket serves as a way to track this idea and potentially implement it.

@nikomatsakis
Copy link
Contributor

Reasons to make this change:

  • immutable values can be implicitly copied into closures without fear of confusing people
  • mutable values can still be copied via explicit capture clause
  • feels analogous to struct fields and the like
  • no more typing than before, just more clarity

@nikomatsakis
Copy link
Contributor

I noticed that the words "mutable" and "local variable" do not appear in this bug! It took me a while to find it as a result. Therefore, I am adding this comment to make future searches easier.

@marijnh
Copy link
Contributor

marijnh commented Dec 14, 2011

immutable values can be implicitly copied into closures without fear of confusing people

Not really. If you close of let x = {mutable foo: int};, you can still observe that a copy was made.

@nikomatsakis
Copy link
Contributor

That's true. This is because vectors are unique pointers. I was tacitly assuming that the "no implicit copies" proposal is accepted, in which case (a) vectors would not be unique pointers and (b) unique pointers (and other structures with expensive copies) would require an explicit copy annotation.

@marijnh
Copy link
Contributor

marijnh commented Dec 14, 2011

The example I gave did not contain any vector. But I guess it would make sense for mutable structure types to also no longer be implicitly copyable.

@nikomatsakis
Copy link
Contributor

Yes, you are right, it did not. I misread it as [mutable...] not {mutable:}. But anyway it would still be addressed by the no implicit copies proposal, which is specifically aimed at preventing explicit copies of (potentially) mutable data like this. (of course @{mutable f: ...} would be implicitly copyable, as it is simply a pointer)

@ssylvan
Copy link

ssylvan commented Jan 22, 2012

Why not just use the existing "mutable" keyword? Even requiring an explicit type wouldn't be so bad, but some simple shorthands would help (e.g. "let x : mutable" would constrain the type to be mutable, but would get the rest of its type from inference (e.g. "mutable int" or whatever)).

I kind of like that you have to type a few more characters for mutable variables. It adds some slight pressure to avoid them.In my one simple rust app (a raytracer) I found that over 90% of local variables were immutable.

Also, IMO, mutable variables are a significant detriment to readability. The more times a variable is changed, the more "what-ifs" you have to consider when interpreting its potential values when reading the code. Making it slightly painful (though not too painful!) to allow mutation could be a good thing.

@brson
Copy link
Contributor

brson commented Jan 23, 2012

Maybe just use mutable in place of let, like mutable x: int = .... That places 'mutable' in the same position relative to the identifier as in records.

@nikomatsakis
Copy link
Contributor

@brson this was the original proposal. I just wanted to change the mutable keyword to mut because mutable is too long.

@brson
Copy link
Contributor

brson commented Jan 23, 2012

oh. carry on then

@graydon
Copy link
Contributor

graydon commented Jan 27, 2012

I'm ok with:

  • Switching mutable to mut, It's already chatty enough when writing record fields.
  • Making let x = ... immutable, requiring mut x = ... to declare a mutable local.

Though var might be a bit more familiar. {var x: int, y: int} clear enough? Or should we keep it as {mut x: int, y: int}?

@brson
Copy link
Contributor

brson commented Jan 27, 2012

I like var because it is more familiar for locals and because mut sounds ugly.

@nikomatsakis
Copy link
Contributor

I don't have a strong opinion on the choice of keyword, though I'd rather use the same one for all mutable things (i.e., {var x: int}).

@marijnh
Copy link
Contributor

marijnh commented Jan 28, 2012

+1 for var

@graydon
Copy link
Contributor

graydon commented Feb 6, 2012

wishy-washy on the matter. I kinda like that mut is ugly, but var seems tolerable if it's the price for making more locals immutable by default :)

@marijnh
Copy link
Contributor

marijnh commented Feb 6, 2012

(As an aside, we already distinguish between mutated locals and immutably used ones—the mut_map that mut.rs produces—so no further optimizations will result from this. For expressing intent, it's probably very useful.)

@ssylvan
Copy link

ssylvan commented Feb 6, 2012

My preference would be to make it roughly "twice as ugly" as immutable locals, whatever that means :-).. I'd want it to be pretty damn ugly so people would avoid it if they don't really need it, and so that it stands out better when you read it, but not ugly enough that you'd feel it was clunky for the few cases where you do need it. For me that probably means I'd prefer the full "mutable" keyword instead of "mut" or "var". Easy enough to change the name of a keyword in retrospect though, the important part is making the distinction at all.

@nikomatsakis
Copy link
Contributor

Personally, I disagree with all this mutable bashing. I would prefer
mutability to be explicit but not painful. I am indifferent as to var
vs mut but a 3-letter keyword makes sense, so that it lines up well
with let.

@kud1ing
Copy link

kud1ing commented Feb 7, 2012

let x; var y;

... even though i understand the proposal, i think i'd have to make a significant effort to come to the mental conclusion "x and y are both variables, var declares a mutable variable".

let x; mut y;
is probably easier to mentally process.

What would you think about having both let mutable x; and mut x;?

The first serves consistency with other uses of "mutable", the second would serve the lazy.
I think i would use both.

@nikomatsakis
Copy link
Contributor

I've been thinking this over and I think I've developed a reasonably strong preference for:

let x = 5; // immutable
let mut x = 10; // mutable

This dovetails nicely with @mut 5.

@marijnh
Copy link
Contributor

marijnh commented Feb 10, 2012

I've been thinking that {var x: int, var y: int} would look very strange. So I think mut is better, an let mut looks good to me.

@graydon
Copy link
Contributor

graydon commented Feb 15, 2012

Agreed. Consensus? :)

@brson
Copy link
Contributor

brson commented Feb 15, 2012

Yes.

@nikomatsakis
Copy link
Contributor

Fixed in dc07280

nikomatsakis added a commit that referenced this issue May 7, 2012
bjorn3 added a commit to bjorn3/rust that referenced this issue Oct 23, 2022
Use pull instead of push based model for getting dylib symbols in the jit
coastalwhite pushed a commit to coastalwhite/rust that referenced this issue Aug 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

7 participants