fsharp / fslang-suggestions

The place to make suggestions, discuss and vote on F# language and core library features
345 stars 21 forks source link

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

Open jbtule opened 5 years ago

jbtule commented 5 years ago

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:

Please tick all that apply:

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'.
jbtule commented 5 years ago

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

cartermp commented 5 years ago

Seems like a reasonable addition to me.