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

Define properties using `let public` #811

Open
charlesroddie opened this issue Nov 26, 2019 · 0 comments
Open

Define properties using `let public` #811

charlesroddie opened this issue Nov 26, 2019 · 0 comments

Comments

@charlesroddie
Copy link

@charlesroddie charlesroddie commented Nov 26, 2019

Define properties using let public

An example class with current syntax:

type A() =
    let u = 0
    let v = 1
    member _.U = u
    member t.W(i) = t.X + i + v
    member t.X(i) = if i = 0 then 1 else t.X(i-1) * v
    static member Y(i) = i + v

I propose we add let public syntax to expose properties. The class could then be rewritten as:

type A() =
    let public U = 0
    let v = 1
    let rec public X(i) = if i = 0 then 1 else X(i-1) * v
    let public W(i) = X + i + v
    static let public Y(i) = i + v

Pros and Cons

Pros:

  1. a) The common pattern let u = ... followed by member _.U = u can be replaced by the single line let public u = ....
    b) The cumbersome _. unused bindings are not present. (They would also be removed if #626 is implemented.)
  2. Sequential ordering with explicit recursion is enforced, as usual in F# code, with the usual benefit of being able to understand code by reading it in sequentially. (Note that the second version swaps W and X and is easier to understand as a result.)

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:

  1. The public API of a class can no longer be found by looking at the bottom of the class definition.
  2. It's another way of doing the same thing, and properties with setters would still need to be implemented with the current member... syntax.

Notes

let public U = f() would be equivalent to let u = f() followed by member _.U = u, so f() is only evaluated once.

There would be no way to replicate member _.SayHello = printf "hello world" using this syntax, although member _.SayHello() = printf "hello world" could be done as let 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:

  • [ 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
  • [ x ] I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • [ x ] 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:

  • [ x ] This is not a breaking change to the F# language design
  • [ x ] I or my company would be willing to help implement and/or test this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.