You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
FS0193 when constraining a type parameter by different instanciations of the same interface #12814
changed the title [-]FS1093 when constraining a type parameter with different instanciations of the same interface[/-][+]FS1093 when constraining a type parameter by different instanciations of the same interface[/+]on Mar 9, 2022
I found that this kind of works with interfaces that implement two interfaces, but needs extra casting because once the compiler identifies that the generic variable 'context is of type IA<int>, it can no longer be of type IA<float>. Almost like the generic 'context isn't actually IABoth.
type IA<'a> =
abstract member GetA: unit -> 'a
type IABoth =
// If you flip these arround you'll get a compile error in IA<int> instead of IA<float>
inherit IA<int>
inherit IA<float>
type TestError() =
member this.Get (c: 'context when 'context :> IABoth) : float =
// Compile Error
let m = (c :> IA<float>).GetA()
let n = (c :> IA<int>).GetA() |> float
n + m
type TestSuccess() =
member this.Get (c: 'context when 'context :> IABoth) : float =
// Works just fine, c appears to be #IABoth instead of IABoth
let m = (c :> IABoth :> IA<float>).GetA()
let n = (c :> IA<int>).GetA() |> float
n + m
I hope this is simply a bug, but a part of me wonders if this will end up being an incompatibility with F#'s type system/type inference.
changed the title [-]FS1093 when constraining a type parameter by different instanciations of the same interface[/-][+]FS0193 when constraining a type parameter by different instanciations of the same interface[/+]on Mar 14, 2022
Yes, this is by design, or can be turned into a language suggestion or linked to an existing one (short summary: there are limitations on generic constraints by multiple different interface instantiations, and lifting these limitations would make F# type inference less useful in practice)
@dsyme, could you confirm about the part in the workaround from @michaeloyer:
(c :> IABoth :> IA<float>).
could this just work with direct safe cast to IA<float>? otherwise, I could see it may be leading to really interesting coding patterns, when dealing with frameworks having deep multi-inheritance hierarchies, just to reach members that remain hidden otherwise.
Would it require a change to the definition :> operator to support this or is it a type checker thing, and hence, potentially suffer from limitation around type inference?
Ideally, this part of the workaround should not be required, nor be a breaking change to get there, is it too hopeful?
Activity
[-]FS1093 when constraining a type parameter with different instanciations of the same interface[/-][+]FS1093 when constraining a type parameter by different instanciations of the same interface[/+]michaeloyer commentedon Mar 12, 2022
I found that this kind of works with interfaces that implement two interfaces, but needs extra casting because once the compiler identifies that the generic variable
'contextis of typeIA<int>, it can no longer be of typeIA<float>. Almost like the generic'contextisn't actuallyIABoth.I hope this is simply a bug, but a part of me wonders if this will end up being an incompatibility with F#'s type system/type inference.
KevinRansom commentedon Mar 14, 2022
@dsyme, this looks by design to me, what are your thoughts?
[-]FS1093 when constraining a type parameter by different instanciations of the same interface[/-][+]FS0193 when constraining a type parameter by different instanciations of the same interface[/+]dsyme commentedon Mar 28, 2022
Yes, this is by design, or can be turned into a language suggestion or linked to an existing one (short summary: there are limitations on generic constraints by multiple different interface instantiations, and lifting these limitations would make F# type inference less useful in practice)
smoothdeveloper commentedon Mar 28, 2022
@dsyme, could you confirm about the part in the workaround from @michaeloyer:
could this just work with direct safe cast to
IA<float>? otherwise, I could see it may be leading to really interesting coding patterns, when dealing with frameworks having deep multi-inheritance hierarchies, just to reach members that remain hidden otherwise.Would it require a change to the definition
:>operator to support this or is it a type checker thing, and hence, potentially suffer from limitation around type inference?Ideally, this part of the workaround should not be required, nor be a breaking change to get there, is it too hopeful?