question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

error FS0073: internal error: Undefined or unsolved type variable when using records with generic type variables and backgroundTask

See original GitHub issue

Getting 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:

  1. 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:closed
  • Created 2 years ago
  • Reactions:14
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
dsymecommented, Sep 9, 2022

@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 the AfterCode<,>.

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 of backgroundTask and I couldn’t find a workaround (when using a generic backgroundTask as in the small repro above).

                        let sm = sm // copy contents of state machine so we can capture it

                        Task.Run<'T>(fun () ->
                            let mutable sm = sm // host local mutable copy of contents of state machine on this thread pool thread
                            sm.Data.MethodBuilder <- AsyncTaskMethodBuilder<'T>.Create ()
                            sm.Data.MethodBuilder.Start(&sm)
                            sm.Data.MethodBuilder.Task)))
1reaction
JohSandcommented, Aug 4, 2022

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(**)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Background task with result leads to compiler error FS0073
I am new to F# language but as far as I read backgroundTask syntax ... Error FS0073 internal error: Undefined or unsolved type...
Read more >
Compiler Error CS0819
Compiler Error CS0819 · To correct this error. There are three options: If the variables are of the same type, use explicit declarations....
Read more >
Compiler Error CS8130
In this article ... Cannot infer the type of implicitly-typed deconstruction variable. Example. The following sample generates CS8130: C# Copy.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found