- Overview
- Tutorials
- Getting started
- Get started with Canton and the JSON Ledger API
- Get Started with Canton, the JSON Ledger API, and TypeScript
- Get started with Canton Network App Dev Quickstart
- Get started with smart contract development
- Basic contracts
- Test templates using Daml scripts
- Build the Daml Archive (.dar) file
- Data types
- Transform contracts using choices
- Add constraints to a contract
- Parties and authority
- Compose choices
- Handle exceptions
- Work with dependencies
- Functional programming 101
- The Daml standard library
- Test Daml contracts
- Next steps
- Application development
- Getting started
- Development how-tos
- Component how-tos
- Explanations
- References
- Application development
- Smart contract development
- Daml language cheat sheet
- Daml language reference
- Daml standard library
- DA.Action.State.Class
- DA.Action.State
- DA.Action
- DA.Assert
- DA.Bifunctor
- DA.Crypto.Text
- DA.Date
- DA.Either
- DA.Exception
- DA.Fail
- DA.Foldable
- DA.Functor
- DA.Internal.Interface.AnyView.Types
- DA.Internal.Interface.AnyView
- DA.List.BuiltinOrder
- DA.List.Total
- DA.List
- DA.Logic
- DA.Map
- DA.Math
- DA.Monoid
- DA.NonEmpty.Types
- DA.NonEmpty
- DA.Numeric
- DA.Optional
- DA.Record
- DA.Semigroup
- DA.Set
- DA.Stack
- DA.Text
- DA.TextMap
- DA.Time
- DA.Traversable
- DA.Tuple
- DA.Validation
- GHC.Show.Text
- GHC.Tuple.Check
- Prelude
- Smart contract upgrading reference
- Glossary of concepts
DA.Record¶
Exports the record machinery necessary to allow one to annotate code that is polymorphic in the underlying record type.
Typeclasses¶
class GetField x r a where
GetField x r a
provides the getter part ofHasField
- getField
: r -> a
class SetField x r a where
SetField x r a
provides the setter part ofHasField
- setField
: a -> r -> r
Data Types¶
- type HasField x r a
= (GetField x r a, SetField x r a)
HasField
is a class synonym forGetField
andSetField
, which respectively give you getter and setter functions for each record field automatically.In the vast majority of use-cases, plain Record syntax should be preferred:
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>
For more on Record syntax, see https://docs.daml.com/daml/intro/3_Data.html#record.
GetField x r a
andSetField x r a
are typeclasses taking three parameters. The first parameterx
is the field name, the second parameterr
is the record type, and the last parametera
is the type of the field in this record. For example, if we define a type:data MyRecord = MyRecord with foo : Int bar : Text
Then we get, for free, the following GetField and SetField instances:
GetField "foo" MyRecord Int SetField "foo" MyRecord Int GetField "bar" MyRecord Text SetField "bar" MyRecord Text
If we want to get a value, we can use the
getField
method of classGetField
:getFoo : MyRecord -> Int getFoo r = getField @"foo" r getBar : MyRecord -> Text getBar r = getField @"bar" r
Note that this uses the “type application” syntax (
f @t
) to specify the field name.Likewise, if we want to set the value in the field, we can use the
setField
method of classSetField
:setFoo : Int -> MyRecord -> MyRecord setFoo a r = setField @"foo" a r setBar : Text -> MyRecord -> MyRecord setBar a r = setField @"bar" a r