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:
[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] 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
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
Use type inference from
with get()
for to help inferset 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 propertiesDataCode
andDescription
The existing way of approaching this problem in F# is to add an extra hint for the set value. As shown:
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.