Closed
Description
When using nameof
in a pattern match, I'd expect the compiler to infer the argument type as string
since nameof
returns a string
. However, the argument is treated as generic and generates invalid IL.
Repro steps
// Expected:
// FS0001: This expression was expected to have type 'int' but here has type 'string'
// Actual:
// No compile error; InvalidProgramException at runtime.
match 3 with
| nameof int -> true
| _ -> false
// Expected inferred type:
// val f : x:string -> bool
// Actual inferred type:
// val f : x:'a -> bool
let f x = match x with nameof x -> true | _ -> false
According to the RFC for the nameof pattern feature, the nameof expr
should be "equivalent to a literal string pattern" which is not being respected.
Expected behavior
Expect the compiler to infer argument type as string
when nameof
is used in a pattern match.
Actual behavior
Compiler infers argument type as generic argument when nameof
is used in a pattern match.
Known workarounds
Explicit type annotation.
let f (x:string) = match x with nameof x -> true | _ -> false
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Done