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

Use type inference from `with get()` to help infer `set value` #794

Open
jbtule opened this issue Oct 3, 2019 · 2 comments

Comments

@jbtule
Copy link

commented Oct 3, 2019

Use type inference from with get() for to help infer set value

I propose we use type inference for a property getter and use it to help infer its matching setter value.

It would allow the StructureType, in the following simplistic example, to define it's get and set implementations abstractly requiring only one type hint (explicit or inferred) rather than two.
See properties DataCode and Description

namespace DyanmicDataModel

open System.Collections.Generic
open System.Runtime.CompilerServices

module Def =
    let dataInt: int -> unit = ignore
    let dataString: string -> unit = ignore

[<AbstractClass>]
type DynamicImplementation() =
    let props = Dictionary<string,obj>()
    member __.GetValue([<CallerMemberName>] ?memberName: string) : 'T =
        let key = memberName |> Option.defaultWith (fun () -> failwith "memberName required")
        let valid,value = props.TryGetValue (key)
        if valid then
            unbox value
        else
            failwith "missing value for 'memberName'"
    member __.SetValue(value:'T, [<CallerMemberName>] ?memberName: string) : unit =
        let key = memberName |> Option.defaultWith (fun () -> failwith "memberName required")
        props.[key] <- box value

type StructureType () =
    inherit DynamicImplementation()
    member this.Definitions () =
        Def.dataInt this.DataCode
        Def.dataString this.Description
        ()
    member this.DataCode
        with get() = this.GetValue ()
        and set value = this.SetValue value
    member this.Description
        with get() = this.GetValue ()
        and set value = this.SetValue value

The existing way of approaching this problem in F# is to add an extra hint for the set value. As shown:

type StructureType () =
    inherit DynamicImplementation()
    member this.Definitions () =
        Def.dataInt this.DataCode
        Def.dataString this.Description
        ()
    member this.DataCode
        with get() = this.GetValue ()
        and set value = this.SetValue (value:int)
    member this.Description
        with get() = this.GetValue ()
        and set value = this.SetValue (value:string)

Pros and Cons

The advantages of making this adjustment to F# is not having to an add extra type hint that seems redundant.

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

Extra information

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

S 🤷‍♂

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

Since this is a compiler error currently, when it infers set value takes an object, as types don't match, I don't believe this is a breaking change.

Error	FS3172	A property's getter and setter must have the same type. Property 'DataCode' has getter of type 'int' but setter of type 'obj'.
Error	FS3172	A property's getter and setter must have the same type. Property 'Description' has getter of type 'string' but setter of type 'obj'.
  • I or my company would be willing to help implement and/or test this
@jbtule

This comment has been minimized.

Copy link
Author

commented Oct 3, 2019

We also have a larger opensource project that could be used for testing.
https://github.com/ekonbenefits/NachaSharp/tree/master/NachaSharp/NachaRecords

@cartermp

This comment has been minimized.

Copy link
Member

commented Oct 3, 2019

Seems like a reasonable addition to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.