-
Notifications
You must be signed in to change notification settings - Fork 22
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
Upcast branches of match
and if
expressions when type is specified
#792
Comments
match
and if
expressions when type is specified
Currently, in ASP.NET controllers using F#, manual upcasting is required:
It would be good to have automatic upcasting when type is specified. |
Not an ideal suggestion but you can simplify it a bit using inferred types: member this.Get(id: int): IActionResult =
match this.Repository.Get(id) with
| null -> this.NotFound() :> _
| _ -> this.Ok(x) :> _ |
I can definitely see this being controversial, since adding anything about making it easier to work with inheritance hierarchies always draws some allergic reactions. I'm generally in favor of something like this since it makes working with unavoidable and common APIs a bit easier. |
I would feel uncomfortable about this feature, or at least there would need to be some explicit guidance needed. Check out this old blog post from Lincoln Atkinson (ex F# compiler team) https://latkin.org/blog/2017/05/02/when-the-scala-compiler-doesnt-help/ looking at the very first sample. I definitely don't want that in F# with the compiler trying to be helpful but picking some arbitrary common type. |
In this case it’s not quite analogous; in Scala the compiler walks up the subtype tree and “magically” finds an ancestor that works. The proposal here is about when the type is already known, not about inferring up the tree. Though this could arguably be a step down the path of more inheritance-oriented programming, which goes against general F# principles/dogma
…On Sat, Oct 5, 2019, at 14:05, Isaac Abraham wrote:
I would feel uncomfortable about this feature, or at least there would need to be some explicit guidance needed. Check out this old blog post from Lincoln Atkinson (ex F# compiler team) https://latkin.org/blog/2017/05/02/when-the-scala-compiler-doesnt-help/ looking at the very first sample. I definitely don't want that in F# with the compiler trying to be helpful but picking some arbitrary common type.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub <#792?email_source=notifications&email_token=ABQEJTTSGFR7VAOLR3KSSVTQND6S5A5CNFSM4I4W3ETKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAN3YCI#issuecomment-538688521>, or mute the thread <https://github.com/notifications/unsubscribe-auth/ABQEJTTZQPX2JUDTORI7SBLQND6S5ANCNFSM4I4W3ETA>.
|
Wouldn't it be kind of a special case of a flexible type, but in the return definition instead of the parameter one? Maybe we could use that to properly signal that we want the return type to be exactly this one. Wonder how much we could reuse of the current flexible type implementation for this, if any. F# docs reference: https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/flexible-types |
@cartermp as long as the behaviour would be predictable, understandable and easy to reason about I'd probably be interested in this eg if there was a type hint to explicitly tell the compiler what the expected type was. |
In fact I remember when writing GPWF having to explain this exact scenario. So perhaps doing this in a specific scope has some value after all 😊 |
This suggestion would just extend current let l1 = [View(); Label()] // Not OK
let l2:View list = [View(); Label()] // OK
let f(_:View list) = ()
f [View(); Label()] // OK A more complete solution would be to upcast whenever the required type is known: let l3:View list = [Label(); Label()] // OK
let v1:View = if true then Label() else Label() // This suggestion would make this OK
let v2:View = Label() // Not OK. Should it be OK? |
@charlesroddie mmm good question. The latter would I assume be logical to permit if the first two are legal, yet that's nothing that's been allowed this far. |
This has been completed as part of RFC FS-1093 https://github.com/fsharp/fslang-design/blob/main/preview/FS-1093-additional-conversions.md, currently in preview and scheduled for F# 6.0 |
F# should upcast return values of
if
andmatch
expressions where the required type is known. For example the following expressions should compile:Currently up-casting (where the intended type is specified or known) happens automatically in lists (
let l:View list = [ Label() ]
) and in function inputs. This doesn't happen inif
andmatch
constructs. There may be other constructs where this also doesn't happen, which should be added to this suggestion.Pros and Cons
The advantages of making this adjustment to F# are: less manual upcasting
The disadvantages of making this adjustment to F# are: none
Extra information
Estimated cost (XS, S, M, L, XL, XXL): M
Related suggestions: There are suggestions also to do with upcasting which are more complex: #3, #536 .
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: