Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F#记录构造函数函数_F#_Applicative_Fsharpx - Fatal编程技术网

F#记录构造函数函数

F#记录构造函数函数,f#,applicative,fsharpx,F#,Applicative,Fsharpx,有没有办法在F#中调用F#记录类型的构造函数 我的动机是我一直在使用来自FSharpx的应用程序验证,但我发现自己编写了很多只构建记录的样板函数 例如,buildAddress函数就是简单的样板文件,如果可能的话,我很想去掉它 let buildAddress streetNumber streetLine1 streetLine2 suburb state postcode = { Address.StreetNumber

有没有办法在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
让buildAddress streetNumber streetLine1 streetLine2郊区州邮政编码=
{
Address.StreetNumber=街道编号
街线1=街线1
街线2=街线2
郊区
状态=状态
邮政编码=邮政编码
}
让我们验证一下地址街道编号街道1街道2郊区州邮政编码=
构建地址
街道号码
成功之路1
成功之路2
成功郊区
成功状态
验证邮政编码
F#记录实际上是通过构造函数生成的,但它似乎无法从F#code中获得。CompilationMapping属性可能会处理这个问题,因为构造函数本身没有任何属性

您可以使用标准的.NET反射(
Activator.CreateInstance
)或FSharp反射(
FSharpValue.MakeRecord
FSharpValue.precomputerRecordConstructor
将为您提供一个要使用的函数)。但是,他们都希望将参数作为装箱对象数组来获取,因此它与应用程序验证不匹配

我认为你不会得到比现在更干净的解决方案