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

Allow ValueOptions to be used as optional parameter types #1136

Open
5 tasks done
Happypig375 opened this issue Apr 29, 2022 · 6 comments
Open
5 tasks done

Allow ValueOptions to be used as optional parameter types #1136

Happypig375 opened this issue Apr 29, 2022 · 6 comments

Comments

@Happypig375
Copy link
Contributor

Happypig375 commented Apr 29, 2022

I propose we allow:

type C(?a) =
    let b = defaultValueArg a 2 // Error here
    member _.B = b
error FS0001: This expression was expected to have type
    ''a voption'    
but here has type
    ''b option'    

The existing way of approaching this problem in F# is using reference options.

Pros and Cons

The advantages of making this adjustment to F# are

  1. More structs, less GC pressure
  2. Allows for more optimisation
  3. In line with recent changes to allow ValueOptions to be used as active pattern return types

The disadvantage of making this adjustment to F# is that existing code ?a will have another potential type for all of us to learn about.

Extra information

Estimated cost (XS, S, M, L, XL, XXL): S

Related suggestions: all are ValueOption parity features.

Affidavit (please submit!)

Please tick this by placing a cross in the box:

  • 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

For Readers

If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.

@cartermp
Copy link
Member

Yep, to me this is a "can't see why no" sorta deal

@charlesroddie
Copy link

charlesroddie commented May 2, 2022

There should be a way to do this yes. I don't think ?a:int syntax should be used as that is the syntax for options. (And disambiguation is important not only to minimize ambiguity but also because it is important to be able to write fully-annotated F# code where a reader doesn't need to do type inference or read subsequent lines to understand the meaning of a given line.)

Perhaps type C(??v:int) or type [<StuctOptionalArguments>] C(?v:int).

Semi-related: it's curious that DefaultParemeterValue(ValueNone) doesn't work. It would be good for defaults to match C# by allowing not only constant expressions but also default struct values including ValueNone and parameterless struct constructors.

@Tarmil
Copy link

Tarmil commented May 3, 2022

I think it could be argued that it's not too bad for readers to not be able to tell if ?a:int is an option or a voption, because from a user's point of view, you still call it the same way. But this point is moot, because no amount of type inference can tell even the compiler whether this should be an option or a voption:

type IMyInterface =
    abstract M : ?a:int -> unit

So we do need a separate syntax indeed.

Semi-related: it's curious that DefaultParemeterValue(ValueNone) doesn't work.

Well, since the tag for ValueNone is 0, it's equal to Unchecked.defaultof<T option>. So you don't need DefaultParameterValue: Optional is enough.

@dsyme
Copy link
Collaborator

dsyme commented Jun 16, 2022

I've marked this approved-in-principle. It would be great to have it implemented

@charlesroddie
Copy link

@dsyme
Copy link
Collaborator

dsyme commented Oct 29, 2022

Yup, the best proposal in that RFC is really [<Struct>] ?x : int I think, seems natural all in all. I don't think struct ?x: int flies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants