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

ofCase doesn't support generic union type #190

Closed
wenLiangcan opened this issue Oct 9, 2021 · 2 comments
Closed

ofCase doesn't support generic union type #190

wenLiangcan opened this issue Oct 9, 2021 · 2 comments

Comments

@wenLiangcan
Copy link

Description

Checking case of generic union type throws System.InvalidCastException.

Repro steps

type MyCase<'T> =
    | Case1 of 'T
    | Case2

let intCase1 = Case1 1
intCase1 |> should be (ofCase <@ Case1 @>)

Expected behavior

The testing should be passed.

Actual behavior

The following exception is throwed.

  Failed Test  [49 ms]
  Error Message:
   System.InvalidCastException : Unable to cast object of type 'Case1[System.Int32]' to type 'MyCase`1[System.Object]'.
  Stack Trace:
     at lambda_method34(Closure , Object )
   at FsUnit.Common.isOfCase@50.Invoke(a x)
   at FsUnit.CustomConstraints.OfSameCaseConstraint.ApplyTo[TActual](TActual actual)
   at NUnit.Framework.Assert.That[TActual](TActual actual, IResolveConstraint expression, String message, Object[] args)
   at NUnit.Framework.Assert.That[TActual](TActual actual, IResolveConstraint expression)
   at FsUnit.TopLevelOperators.should[a,a](FSharpFunc`2 f, a x, Object y)

Known workarounds

None.

Related information

  • Windows 11
  • FsUnit 4.0.6
  • .NET 5
@wenLiangcan wenLiangcan changed the title ofCase doesn't support generic union Type ofCase doesn't support generic union type Oct 9, 2021
@sergey-tihon
Copy link
Member

that is how type inference works in F#.
Compiler has no information to identify type of Case1 inside quotation, that is why it uses most generic one MyCase

image

in this case you have to manually specify type to help compiler

image

otherwise types are different and they should not be equal

@wenLiangcan
Copy link
Author

@sergey-tihon Thanks, did not know this way to specify the concrete type for a case before.

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

No branches or pull requests

2 participants