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
Now, what we want to is to have a function that will specialize our env parameter over different variants of IHave<> interface - .NET allows types to implement the same interface multiple times with different generic type parameters:
letrollAndPrint env =letnum= Random.number env
Log.debug env "Rolled %i" num // compile error: "The type `ILogger' does not match the type 'Random'"
Apparently F# compiler interface inference doesn't take generic type parameters into account. Initially I thought I may be able to trick compiler by using interfaces from IHave<T>:
But this also fails with the same exact error. Specifying env type constraints ('e when 'e :> IHave<ILogger> and 'e :> IHave<Random>) explicitly also fails with message: Type constraint missmatch. The type ''e' is not compatible with IHave<Random>.
Implementing different versions of the same interface is permitted in C#, however it's not the case in F#. Example:
This type implements the same interface at different generic instantiations 'IHave' and 'IHave'. This is not permitted in this version of F#.
This pattern is sometimes used in for message handling of different message types, including cases in popular C# libraries like MediatR. I think it could be useful to add this feature to F# as well.
Affidavit
Please tick this by placing a cross in the box:
This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
[] 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:
This is not a breaking change to the F# language design
[] I or my company would be willing to help implement...
...and/or test this with a help of a mentor.
The text was updated successfully, but these errors were encountered:
Consider following example:
Now, what we want to is to have a function that will specialize our
env
parameter over different variants ofIHave<>
interface - .NET allows types to implement the same interface multiple times with different generic type parameters:Apparently F# compiler interface inference doesn't take generic type parameters into account. Initially I thought I may be able to trick compiler by using interfaces from
IHave<T>
:But this also fails with the same exact error. Specifying
env
type constraints ('e when 'e :> IHave<ILogger> and 'e :> IHave<Random>
) explicitly also fails with message: Type constraint missmatch. The type ''e' is not compatible withIHave<Random>
.Implementing different versions of the same interface is permitted in C#, however it's not the case in F#. Example:
fails with error:
This pattern is sometimes used in for message handling of different message types, including cases in popular C# libraries like MediatR. I think it could be useful to add this feature to F# as well.
Affidavit
Please tick this by placing a cross in the box:
Please tick all that apply:
The text was updated successfully, but these errors were encountered: