DA.Record
Daml 模块 DA.Record 参考文档。
DA.Record
导出记录相关机制,使代码能对底层记录类型多态。
模块概览
数据类型
type HasField = (GetField x r a, SetField x r a)
HasField 是 GetField 与 SetField 的类型同义词,分别为每条记录字段自动提供 getter 与 setter。
绝大多数场景应优先使用普通记录语法:
daml> let a = MyRecord 1 "hello"
daml> a.foo
1
daml> a.bar
"hello"
daml> a { bar = "bye" }
MyRecord {foo = 1, bar = "bye"}
daml> a with foo = 3
MyRecord {foo = 3, bar = "hello"}
daml>
更多记录语法见 DA.Record。
GetField x r a 与 SetField x r a 为三参数类型类:第一参数 x 为字段名,第二参数 r 为记录类型,第三参数 a 为该字段类型。
例如定义:
data MyRecord = MyRecord with
foo : Int
bar : Text
则自动得到下列 GetField / SetField 实例:
GetField "foo" MyRecord Int
SetField "foo" MyRecord Int
GetField "bar" MyRecord Text
SetField "bar" MyRecord Text
取值可使用 GetField 的 getField:
getFoo : MyRecord -> Int
getFoo r = getField @"foo" r
getBar : MyRecord -> Text
getBar r = getField @"bar" r
注意这里使用类型应用语法(f @t)指定字段名。
设值可使用 SetField 的 setField:
setFoo : Int -> MyRecord -> MyRecord
setFoo a r = setField @"foo" a r
setBar : Text -> MyRecord -> MyRecord
setBar a r = setField @"bar" a r
类型类
class GetField x r a
GetField x r a 提供 HasField 的 getter 部分。
方法:
getField : r -> a
实例:
instance GetField _1 (a, b) ainstance GetField _1 (a, b, c) ainstance GetField _1 (a, b, c, d) ainstance GetField _1 (a, b, c, d, e) ainstance GetField _2 (a, b) binstance GetField _2 (a, b, c) binstance GetField _2 (a, b, c, d) binstance GetField _2 (a, b, c, d, e) binstance GetField _3 (a, b, c) cinstance GetField _3 (a, b, c, d) cinstance GetField _3 (a, b, c, d, e) cinstance GetField _4 (a, b, c, d) dinstance GetField _4 (a, b, c, d, e) dinstance GetField _5 (a, b, c, d, e) einstance GetField appEndo (Endo a) (a -> a)instance GetField category FailureStatus FailureCategoryinstance GetField errorId FailureStatus Textinstance GetField getAll All Boolinstance GetField getAny Any Boolinstance GetField getAnyView AnyView Anyinstance GetField getAnyViewInterfaceTypeRep AnyView InterfaceTypeRepinstance GetField hd (NonEmpty a) ainstance GetField map (Set k) (Map k ())instance GetField message FailureStatus Textinstance GetField message ArithmeticError Textinstance GetField message AssertionFailed Textinstance GetField message GeneralError Textinstance GetField message PreconditionFailed Textinstance GetField meta FailureStatus (TextMap Text)instance GetField runState (State s a) (s -> (a, s))instance GetField srcLocEndCol SrcLoc Intinstance GetField srcLocEndLine SrcLoc Intinstance GetField srcLocFile SrcLoc Textinstance GetField srcLocModule SrcLoc Textinstance GetField srcLocPackage SrcLoc Textinstance GetField srcLocStartCol SrcLoc Intinstance GetField srcLocStartLine SrcLoc Intinstance GetField tl (NonEmpty a) [a]
class SetField x r a
SetField x r a 提供 HasField 的 setter 部分。
方法:
setField : a -> r -> r
实例:
instance SetField _1 (a, b) ainstance SetField _1 (a, b, c) ainstance SetField _1 (a, b, c, d) ainstance SetField _1 (a, b, c, d, e) ainstance SetField _2 (a, b) binstance SetField _2 (a, b, c) binstance SetField _2 (a, b, c, d) binstance SetField _2 (a, b, c, d, e) binstance SetField _3 (a, b, c) cinstance SetField _3 (a, b, c, d) cinstance SetField _3 (a, b, c, d, e) cinstance SetField _4 (a, b, c, d) dinstance SetField _4 (a, b, c, d, e) dinstance SetField _5 (a, b, c, d, e) einstance SetField appEndo (Endo a) (a -> a)instance SetField category FailureStatus FailureCategoryinstance SetField errorId FailureStatus Textinstance SetField getAll All Boolinstance SetField getAny Any Boolinstance SetField getAnyView AnyView Anyinstance SetField getAnyViewInterfaceTypeRep AnyView InterfaceTypeRepinstance SetField hd (NonEmpty a) ainstance SetField map (Set k) (Map k ())instance SetField message FailureStatus Textinstance SetField message ArithmeticError Textinstance SetField message AssertionFailed Textinstance SetField message GeneralError Textinstance SetField message PreconditionFailed Textinstance SetField meta FailureStatus (TextMap Text)instance SetField runState (State s a) (s -> (a, s))instance SetField srcLocEndCol SrcLoc Intinstance SetField srcLocEndLine SrcLoc Intinstance SetField srcLocFile SrcLoc Textinstance SetField srcLocModule SrcLoc Textinstance SetField srcLocPackage SrcLoc Textinstance SetField srcLocStartCol SrcLoc Intinstance SetField srcLocStartLine SrcLoc Intinstance SetField tl (NonEmpty a) [a]
本文由 CC Privacy Club 根据 Canton Network 官方文档(CC-BY-4.0)整理翻译,仅供学习;实现细节以官方最新版本为准。