Bro*_*nie 5 f# applicative fsharpx
有没有办法在F#中为F#记录类型调用构造函数?
我的动机是我一直在使用FSharpx的应用验证,但发现自己编写了大量的构造记录的样板函数.
buildAddress函数,例如在简单的样板文件中,我希望尽可能摆脱它.
let buildAddress streetNumber streetLine1 streetLine2 suburb state postcode =
{
Address.StreetNumber = streetNumber
StreetLine1 = streetLine1
StreetLine2 = streetLine2
Suburb = suburb
State = state
Postcode = postcode
}
let validateAddress streetNumber streetLine1 streetLine2 suburb state postcode =
buildAddress
<!> isNumber streetNumber
<*> Success streetLine1
<*> Success streetLine2
<*> Success suburb
<*> Success state
<*> validatePostcode postcode
Run Code Online (Sandbox Code Playgroud)
F#记录实际上是使用构造函数生成的,但它似乎不能从F#代码中获得.可能CompilationMapping属性处理它,因为构造函数本身没有任何属性.
你可以做什么,就是让使用标准的.NET反射(构造Activator.CreateInstance
)或FSharp一个(FSharpValue.MakeRecord
,FSharpValue.PreComputeRecordConstructor
将让你的函数使用).所有这些都希望将参数作为盒装对象的数组来获取,因此它不能很好地与应用验证相匹配.
我认为你不会得到比你现在更清洁的解决方案.
归档时间: |
|
查看次数: |
445 次 |
最近记录: |