F#是否了解其歧视工会的汇编表格? [英] Is F# aware of its discriminated unions' compiled forms?
问题描述
F#中的已区分联合被编译为抽象类,并且其选项变为嵌套的具体类.
A discriminated union in F# is compiled to an abstract class and its options become nested concrete classes.
type DU = A | B
DU是抽象的,而DU.A和DU.B是具体的.
DU is abstract while DU.A and DU.B are concrete.
使用ServiceStack,可以使用函数来自定义类型序列化为JSON字符串并返回.关于DU类型,这是我在C#中可以做到的方式.
With ServiceStack, the serialization of types to JSON strings and back can be customized with functions. With respect to the DU type, here's how I could do it in C#.
using ServiceStack.Text;
JsConfig<DU.A>.SerializeFn = v => "A"; // Func<DU.A, String>
JsConfig<DU.B>.SerializeFn = v => "B"; // Func<DU.B, String>
JsConfig<DU>.DeserializeFn = s =>
if s == "A" then DU.NewA() else DU.NewB(); // Func<String, DU>
F#是否了解其歧视工会的汇编表格?在编译时如何获取F#中的DU.A类型?
Is F# aware of its discriminated unions' compiled forms? How would I get the type of DU.A in F# at compile time?
typeof<DU> // compiles
typeof<DU.A> // error FS0039: The type 'A' is not defined
typeof<A> // error FS0039: The type 'A' is not defined
我可以轻松地在F#中注册一个用于反序列化的函数.
I can easily enough register a function for deserialization in F#.
open System
open ServiceStack.Text
JsConfig<DU>.RawDeserializeFn <-
Func<_, _>(fun s -> printfn "Hooked"; if s = "A" then A else B)
对于具体类型DU.A和DU.B,是否可以完全在F#中注册序列化功能?
Is it possible to register serialize functions wholly in F# for the concrete types DU.A and DU.B?
推荐答案
尽管所有行为(抽象类等)不仅是实现细节,而且实际上是由规范定义的,但这些东西在F#中是不可接受的-这是规范的报价
Whilst all the behaviour (the abstract classes etc.) is not just an implemenation detail, it is actually defined by the spec, these things are not accesible from F# - this is a quote from the spec
已编译的联合类型U具有:
A compiled union type U has:
·每个空联合案例都有一个CLI静态获取属性U.C C.此属性获取一个单例对象,该对象表示每个这样的对象 情况.
· One CLI static getter property U.C for each null union case C. This property gets a singleton object that represents each such case.
·每个非null联合案例C都有一个CLI嵌套类型U.C. 类型具有实例属性Item1,Item2.... 联合用例,或者只有一个实例属性Item(如果只有一个) 场地.但是,只有一种情况的已编译联合类型不会 具有嵌套类型.相反,联合类型本身扮演着 案例类型.
· One CLI nested type U.C for each non-null union case C. This type has instance properties Item1, Item2.... for each field of the union case, or a single instance property Item if there is only one field. However, a compiled union type that has only one case does not have a nested type. Instead, the union type itself plays the role of the case type.
·每个非空联合案例C的一个CLI静态方法U.NewC. 这种方法会为这种情况构造一个对象.
· One CLI static method U.NewC for each non-null union case C. This method constructs an object for that case.
·每个案例C都有一个CLI实例属性U.IsC. 属性会根据情况返回true或false.
· One CLI instance property U.IsC for each case C. This property returns true or false for the case.
·每个案例C的一个CLI实例属性U.Tag. 属性会获取或计算与大小写相对应的整数标签.
· One CLI instance property U.Tag for each case C. This property fetches or computes an integer tag corresponding to the case.
·如果U具有不止一种情况,则它具有一种CLI嵌套类型 U.Tags. U.Tags类型在每种情况下都包含一个整数文字,在 从零开始递增顺序.
· If U has more than one case, it has one CLI nested type U.Tags. The U.Tags typecontains one integer literal for each case, in increasing order starting from zero.
·编译的联合类型具有所需的方法 实施其自动生成的接口,以及任何其他功能 用户定义的属性或方法.
· A compiled union type has the methods that are required to implement its auto-generated interfaces, in addition to any user-defined properties or methods.
这些方法和属性可能无法直接在F#中使用. 但是,这些类型具有面向用户的List.Empty,List.Cons, Option.None和Option.Some一些属性和/或方法.
These methods and properties may not be used directly from F#. However, these types have user-facing List.Empty, List.Cons, Option.None, and Option.Some properties and/or methods.
重要的是,"F#可能无法使用这些方法和属性".
Importantly, "these methods and properties may not be used from F#".
这篇关于F#是否了解其歧视工会的汇编表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!