如何通过填写magic来通过此测试?
type DU =
| ACaseName
| BThereCake
let magic (q: Quotation<_>): string =
// smallest F# code in here?
open Expecto
let subject = magic <@ ACaseName @>
Expect.equal subject "ACaseName" "Should extract the NAME of the DU case"发布于 2017-04-26 00:49:23
在这种情况下,可以执行以下操作:
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 @>问题是,当工会案例有一些争论时,你想做什么。例如:
type DU =
| ACaseName
| BThereCake of int如果您想从<@ BThereCake @>中提取名称,而不仅仅是从<@ BThereCake(12) @>中提取名称,则需要再添加一个大小写:
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 @>https://stackoverflow.com/questions/43616531
复制相似问题