fsharp / fsharp/fslang-suggestions

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

Open
#794 2 comments 5 reactions 0 assignees View on GitHub
approved-in-principle area: object-programming
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

# 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`

```fsharp
namespace DyanmicDataModel

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

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

[]
type DynamicImplementation() =
let props = Dictionary()
member __.GetValue([] ?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, [] ?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:
```fsharp
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](http://stackoverflow.com)) and I have searched stackoverflow for discussions of this issue
* [X] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) and believe this is not a duplicate
* [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

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the StructureType example and examining compiler diagnostic FS3172, which currently reports mismatched getter and setter types. Determine the language-design and compiler changes needed so the getter type constrains the setter without its explicit annotation, then add coverage showing both DataCode and Description compile with matching types.

Written by the indexing model from the issue text.

Assessment

Tech stack
fsharp
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.