gpt4 book ai didi

f# - F#是否了解其歧视工会的汇编表格?

转载 作者:行者123 更新时间:2023-12-04 03:01:14 26 4
gpt4 key购买 nike

F#中的已区分联合将被编译为抽象类,并且其选项将成为嵌套的具体类。

type DU = A | B

DU是抽象的,而DU.A和DU.B是具体的。

借助ServiceStack,可以使用函数来自定义类型序列化为JSON字符串并返回。关于DU类型,这是我在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类型?
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#中注册一个用于反序列化的函数。
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#中注册序列化功能?

最佳答案

尽管所有行为(抽象类等)不仅是实现细节,它实际上是由规范定义的,但这些事情在F#中是 Not Acceptable -这是规范的引文

A compiled union type U has:

· One CLI static getter property U.C for each null union case C. This property gets a singleton object that represents each such case.

· 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.

· One CLI static method U.NewC for each non-null union case C. This method constructs an object for that case.

· One CLI instance property U.IsC for each case C. This property returns true or false for the case.

· One CLI instance property U.Tag for each case C. This property fetches or computes an integer tag corresponding to the case.

· 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.

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#不能使用这些方法和属性”。

关于f# - F#是否了解其歧视工会的汇编表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17832203/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com