Case Title DU
1 answer
In this case, the following actions will be performed:
open Microsoft.FSharp.Quotations
let magic (q: Expr<_>): string =
match q with
| Patterns.NewUnionCase(case, args) -> case.Name
| _ -> failwith "Not a union case"
let subject = magic <@ ACaseName @>
The question is what do you want to do when the join case has some arguments. For example:
type DU =
| ACaseName
| BThereCake of int
If you want to extract the name from <@ BThereCake @>
, not just from <@ BThereCake(12) @>
, you need to add another case:
let magic (q: Expr<_>): string =
match q with
| DerivedPatterns.Lambdas(_, Patterns.NewUnionCase(case, args))
| Patterns.NewUnionCase(case, args) -> case.Name
| _ -> failwith "Not a union case"
let subject = magic <@ BThereCake @>
+10
source to share