error FS0073: internal error: Undefined or unsolved type variable when using records with generic type variables and backgroundTask
See original GitHub issueGetting internal error when trying to compile generic type that uses backgroundTask expression.
open System.Text.Json
type Dto = {
DtoValue : string
Key : string
}
type MyGenericType<'Key,'Value> = {
Value : 'Value
Key : 'Key
}
type ProblematicType<'Key, 'Value, 'Dto, 'E>( fromDto : 'Dto -> Result<MyGenericType<'Key,'Value>,'E> ) =
let myTask =
backgroundTask {
let dto = """{"DtoValue":"1","Key":"key1"}""" |> JsonSerializer.Deserialize<'Dto>
return fromDto dto |> printfn "%A"
}
member __.ContainsKey = fun (key: 'Key) -> true
type MyType = MyGenericType<string,int>
module MyType =
let fromDto (dto: Dto) =
try
{
Value = int dto.DtoValue
Key = dto.Key
}
|> Ok
with | e -> Error e
let problem = ProblematicType<string,int,Dto,exn>(MyType.fromDto)
Provide the steps required to reproduce the problem:
- Execute above code in FSI or try to compile in console app with
dotnet build
.
If possible attach a zip file with the repro case. This often makes it easier for others to reproduce. The zip file should ideally represent the situation just before the call/step that is problematic.
Expected behavior
It should compile and run successfully.
Actual behavior
backgroundTask {
--------^^^^^^^^^^^^^^
error FS0073: internal error: Undefined or unsolved type variable: 'Key
Known workarounds
Use task
instead of backgroundTask
expression.
Related information
.NET SDK (reflecting any global.json): Version: 6.0.100 Commit: 9e8b04bbff
Runtime Environment: OS Name: Mac OS X OS Version: 11.5 OS Platform: Darwin RID: osx.11.0-x64 Base Path: /nix/store/nwyx15pwbkfqxl818q2nhh3cmnmvygik-dotnet-sdk-6.0.100/sdk/6.0.100/
Host (useful for support): Version: 6.0.0 Commit: 4822e3c3aa
.NET SDKs installed: 6.0.100 [/nix/store/nwyx15pwbkfqxl818q2nhh3cmnmvygik-dotnet-sdk-6.0.100/sdk]
.NET runtimes installed: Microsoft.AspNetCore.App 6.0.0 [/nix/store/nwyx15pwbkfqxl818q2nhh3cmnmvygik-dotnet-sdk-6.0.100/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 6.0.0 [/nix/store/nwyx15pwbkfqxl818q2nhh3cmnmvygik-dotnet-sdk-6.0.100/shared/Microsoft.NETCore.App]
Issue Analytics
- State:
- Created 2 years ago
- Reactions:14
- Comments:17 (6 by maintainers)
Top GitHub Comments
@JohSand @et1975 @TheAngryByrd I have pushed a fix for this. I apologise for not getting to this earlier.
@JohSand is correct that the problem happens when
ResumableStateMachine<>
is used in a closure in theAfterCode<,>
.I don’t think there’s any workaround if it is necessary to capture the
ResumableStateMachine
struct type in a closure of the AfterCode. For example, this code is part of the implementation ofbackgroundTask
and I couldn’t find a workaround (when using a genericbackgroundTask
as in the small repro above).I ran into this recently, and it seems that it happens when the byref<ResumableStateMachine<>> is captured in a closure in the AfterCode<,>. So Task.Run for backgroundTask, and the closures crated for cancellableTask and coldTask. I have no idea how to work around it though. Main issue is providing a ref of the statemachine to AsyncMethodBuilder.Start(**)