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

Alternative syntax for declaring mutable values #836

Closed
pblasucci opened this issue Feb 8, 2020 · 8 comments
Closed

Alternative syntax for declaring mutable values #836

pblasucci opened this issue Feb 8, 2020 · 8 comments

Comments

@pblasucci
Copy link

@pblasucci pblasucci commented Feb 8, 2020

Title of Suggestion

I propose we support a slightly more consistent (and, thus, simpler) syntax for declaring mutable values. Specifically:

let someVar <- 3

The existing way of approaching this problem in F# is to use the mutable keyword, for example:

let mutable someVar = 3

Pros and Cons

The advantages of making this adjustment to F# are ...

  1. Makes the language pedagogically easier ("use = for bindings; use <- for mutation").

The disadvantages of making this adjustment to F# are ...

  1. It's work
  2. Introduces multiples ways to do something (though there's precedent for that elsewhere in the language)
  3. A lone declaration, in the proposed new syntax, might not "pop" (visually) if its surrounded by immutable bindings (though this might not be a real concern because mutable declarations tend to be rare and/or grouped with other mutable declarations).

Extra information

I realize this is a purely cosmetic suggestion. But feels no less trite than #168.

Also note, I'm not proposing to replace the existing syntax. Although one hopes it becomes deprecated over time (if not formally, than at least casually... c.f. upcast, downcast keywords).

Estimated cost (XS, S, M, L, XL, XXL): M (only because I'm not familiar enough with the compiler to make a decent estimate).

Affidavit (please submit!)

Please tick this by placing a cross in the box:

  • [X ] This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:

  • This is not a breaking change to the F# language design
  • I or my company would be willing to help implement and/or test this
@jwosty

This comment has been minimized.

Copy link
Contributor

@jwosty jwosty commented Feb 8, 2020

I think that having to type out “mutable” to make something mutable is a good thing, as it’s one of the things that helps F# as a language favor immutability over mutability. I’m a fan of it being a little more work to make something mutable.

It’s also more discoverable via search engines the way it is now. Imagine being a new user and seeing:

let x = 42
let y <- “hello world”

It would be confusing and difficult to figure out what the difference is between the two is. You can’t easily Google or Bing that.

@7sharp9

This comment has been minimized.

Copy link
Member

@7sharp9 7sharp9 commented Feb 8, 2020

Theres nothing inherently wrong with mutable if you use it correctly, if anything I think the keyword should be shortened to mut. Being purposely obtuse or difficult with syntax is not going to help anyone.

@abelbraaksma

This comment has been minimized.

Copy link

@abelbraaksma abelbraaksma commented Feb 8, 2020

This is probably one of those parts of the language where people feel very strongly one way or the other, as a personal preference.

I find that I use mutable so rarely, that I still sometimes have to look it up. I mean, to me, it would feel more consistent if it was let mutable x <- 42, similar to each other case you use the mutable value.

And before they got syntax highlighting, I found it rather hard to spot what happened in x <- 56 statements. I know this may be contentious, but I'd like them to jump out of my code more than they do now, as they often describe non standard coding (but debatable, I know ;).

@pblasucci

This comment has been minimized.

Copy link
Author

@pblasucci pblasucci commented Feb 9, 2020

Honestly, I've no real issue with the mutable keyword. The (admittedly limited) awkwardness comes from the equals sign. Put differently, using let mutable x <- 3 would still hit the primary benefit listed for this suggestion.

@Swoorup

This comment has been minimized.

Copy link

@Swoorup Swoorup commented Feb 9, 2020

One time I had to find a bug. Issue turned out to be a race condition due to globally mutable state. Being able to grep for usages of mutable is what narrowed the triaging down significantly. I don't prefer it to be implicit, I find mutability rather better if made explicit.

@charlesroddie

This comment has been minimized.

Copy link

@charlesroddie charlesroddie commented Feb 9, 2020

use = for bindings; use <- for mutation

I wouldn't consider let mutable someVar = 3 to involve a mutation. And so I wouldn't find let mutable someVar <- 3 consistent. I like the requirement to specify mutable.

I do agree that using <- for mutation consistenly would be a good thing, and arguably Label(Text = "hello world") should be let l = Label(Text <- "hello world").

@voronoipotato

This comment has been minimized.

Copy link

@voronoipotato voronoipotato commented Feb 18, 2020

I do not agree with making mutable implicit, when we write a language it's important that we make the opinionated path simple, and there be some amount a friction on others. If you work with a team, it's really useful that they have guiding friction on doing things in a more standard way. If you make nonstandard things too easy people will come to rely on that as a primary way of doing things. Since .NET does not have any guards on ownership of mutability I personally do not think it's particularly easy to use well and actually think that some friction is preferable, even though sometimes I use mutable.

@pblasucci

This comment has been minimized.

Copy link
Author

@pblasucci pblasucci commented Feb 19, 2020

I played with the compiler source (briefly) and decided the effort out-weighs the value.

@pblasucci pblasucci closed this Feb 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
7 participants
You can’t perform that action at this time.