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

HRT encoding doesn't work with multiple generic restrictions #13465

Closed
Lanayx opened this issue Jul 7, 2022 · 2 comments
Closed

HRT encoding doesn't work with multiple generic restrictions #13465

Lanayx opened this issue Jul 7, 2022 · 2 comments
Labels

Comments

@Lanayx
Copy link
Contributor

Lanayx commented Jul 7, 2022

I'm trying to use special encoding that was recommended by @dsyme in fsharp/fslang-suggestions#567 (comment) together with generic types that have multiple restrictions and fail to do this

Sharplab link

Provide the steps required to reproduce the problem:

Code that works fine:

open System

type I1 = 
    abstract member i1 : int -> int
    
type I2 = 
    abstract member i2 : float -> float
    
//https://github.com/fsharp/fslang-suggestions/issues/567
type HRTEncoding = 
    abstract member Eval<'T> : unit -> 'T

type Cache<'T> (getGenericValue: HRTEncoding -> 'T) =
    member this.GetValue(env: HRTEncoding) =
        getGenericValue env

let x = {
    new HRTEncoding with 
        member this.Eval () = "test" |> box |> unbox
}

let test (env: 'T when 'T :> I1) =
    env.i1(DateTime.Now.Day)

let y = Cache(fun env ->
    let evaled = env.Eval()
    test evaled)
    
let z = y.GetValue(x)

Change when 'T :> I1 to when 'T :> I1 and 'T :> I2

Expected behavior

It works, just one more generic restriction is added

Actual behavior

error FS0331: The implicit instantiation of a generic construct at or near this point could not be resolved because it could resolve to multiple unrelated types, e.g. 'I2' and 'I1'. Consider using type annotations to resolve the ambiguity
error FS0071: Type constraint mismatch when applying the default type 'I1' for a type inference variable. The type 'I1' is not compatible with the type 'I2' Consider adding further type constraints

Known workarounds

Not use this approach

Related information

Provide any related information (optional):

  • .NET 6.0.202
  • Fsharp 6.0.4
@Lanayx Lanayx added the Bug label Jul 7, 2022
@Lanayx
Copy link
Contributor Author

Lanayx commented Jul 7, 2022

Closing as not a bug :) Managed to solve the issue

@Lanayx Lanayx closed this as completed Jul 7, 2022
@Lanayx
Copy link
Contributor Author

Lanayx commented Jul 8, 2022

Update: I didn't manage to solve the issue :) However I'm still unsure if it is a bug or by design

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant