-
Notifications
You must be signed in to change notification settings - Fork 21
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
Define properties using let public
#811
Comments
This creates a troubling inconsistency though: |
Does that make an observable difference to anything @Tarmil or is this just an implementation detail? This issue could be changed to refer to public fields instead of properties if that gives greater consistency? |
@charlesroddie, I'm personally liking the current dichotomy between Regarding the pros:
The cons + my mitigation of the pros are making this suggestion a no go (as in no upvote from me). |
In F# it's standard to have a linear structure where definitions depend only on previous definitions. Currently, because of the cumbersomeness of defining things and then creating members out of them, there is an incentive to use If removing Relative to that situation, yes this suggestion would just avoid the need to write I wanted to explore whether the language property of encouraging linear structures and concision which is present in the rest of the language could be maintained inside class definitions. I understand that this suggestion is a larger syntax change than the language would usually make. |
@charlesroddie thanks for additional context, the actual core motivation wasn't clear to me, or rather, your additional comment makes it more explicit / helps with my reading of your suggestion. you'd like to enforce order of declaration to 100% of the code, while removing need to define the boiler plate members on custom types. I think the idioms and design choice of having members being I personally see the F# enforced order of declaration as nice to have, especially with short If this is a really important concern, working on an analyzer/lint making sure that members are no more than a single short expression or even a sole symbol would get you there, albeit with the cons of the boilerplate oneliners which you highlight, but I think it is minimal burden. I just upvoted #626 as I believe it is helping a bit while not breaking the strong dichotomy between |
On consideration, the implementation would be similar to Similarly The similarity to
I think |
@charlesroddie, regarding the semantics of creating a backing field exposed by a property, it indeed makes more sense to reuse the Considering your main concern is enforced order of declaration, what about one or a mix of those alternative approaches:
|
Define properties [edit: and public fields] using
let public
An example class with current syntax:
I propose we add
let public
syntax to expose properties. The class could then be rewritten as:Pros and Cons
Pros:
let u = ...
followed bymember _.U = u
can be replaced by the single linelet public u = ...
.b) The cumbersome
_.
unused bindings are not present. (They would also be removed ifmember P = expr
#626 is implemented.)Current F# class code is a tradeoff between 1. and 2. If you use more
let u = ...; member _.U = u
you get the benefits of code order at the expense of verbosity. This suggestion would remove that tradeoff.Cons:
member...
syntax.Notes
let public U = f()
would be equivalent tolet u = f()
followed bymember _.U = u
, sof()
is only evaluated once.There would be no way to replicate
member _.SayHello = printf "hello world"
using this syntax, althoughmember _.SayHello() = printf "hello world"
could be done aslet public SayHello() = ...
. Arguably this is an advantage as the fact that properties without arguments execute their bodies whenever called is a fact that can surprise beginners and catch out experienced users.Extra information
Estimated cost (XS, S, M, L, XL, XXL):
L
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
The text was updated successfully, but these errors were encountered: