Skip to content
CCPEDIAby Unity Nodes
VideosLesson 8: Typeclasses and Records

Lesson 8: Typeclasses and Records

Nothing is loaded from YouTube until you press play, and then without tracking cookies.

About this video

01:12 Data Records 03:38 Custom typeclass 05:19 Review

For a better learning experience, sign up and enroll in this free course at: daml.talentlms.com/plus/catalog/courses/141

Transcript

951 words

Captions as YouTube publishes them, usually machine-generated, so names such as Daml and Canton can be misheard.

  1. 0:01

    Typeclasses are classes or categories of data types. A typeclass is like an interface for a function that a data type can implement. If you're familiar with object oriented programming, typeclasses come close to the concept of interfaces that have one or more function signatures that are then implemented by a specific class to exhibit that behavior.

  2. 0:23

    For example, "show" which we've been using to print the value of a variable, is actually a typeclass that Daml data types have implemented. When a data type implements a function defined by a typeclass, we say that the data type is an instance of that typeclass.

  3. 0:38

    So the int data type is an instance of the show typeclass because it is implemented the show function of the typeclass show. There are many other typeclasses available in Daml. For example, Eq enables comparing two values of a certain data type to check if they're equal or not. Ord is a typeclass that enables sorting.

  4. 1:00

    We can write our own typeclasses, but to do that, we need to first learn how to write our own data type. Daml provides a mechanism to do just that using data records. A data record is a data structure that contains one or more named fields of different data types. So in a way, a data record is similar to a tuple, but with its fields having a name.

  5. 1:24

    The syntax requires that we declare a record with the keyword data, which means that we are defining a new data type. The name before the equal sign indicates the name of the data type, and the name after indicates its constructor. The data type name and the constructor name must be the same.

  6. 1:43

    The with block contains the fields and their data types. The example shown here is personal record that has two fields, name of type text and ID of type int. Now let's use this data type to define a variable. In the script below we create a variable, some data of type personal.

  7. 2:05

    We assign values to its field, but when we try to print it using a debug statement, we get a compilation error that says no instance for show personal record. That's because our data type personal record doesn't have the ability to be read as text, or in other words, the data type personal record that we created isn't an instance of the show typeclass.

  8. 2:26

    And as a consequence, Daml can't create textual representations of personal record and. To fix that, we need to make it an instance of the show typeclass. We can make this happen using the deriving keyword and list the typeclasses you want this data type to inherit. And in this case, it's the show typeclass.

  9. 2:46

    Once we do that, the compilation error goes away and we can now run the script to see the output. If we want the personal record values to be comparable, then we can make it an instance of EQ typeclass by adding EQ to the deriving clause.

  10. 3:03

    To check how this works, we add another variable called some other data of personal record type, but with different values in its fields. Now we can compare them to see if they are the same in the script. By printing the value of some data is equal to some other data. As you can see, the result is false because they're different.

  11. 3:22

    If the values for name and ID are the same in some data and some other data records, then the equality statement would return true. So what we've seen so far is how to make a custom data type and how to make it an instance of an existing typeclass. But what if we want to create a custom typeclass?

  12. 3:41

    Let's say that we want the personal record type to have a behavior through which it can be encrypted. So in this example, we first define a typeclass encryptable. A typeclass definition starts with the keyword class followed by its name, an input, and an output.

  13. 3:58

    The where block has the name of the function that the Encryptable instances need to implement. In this case, the function is encrypt, which takes an input I and returns a value. o. The next step is to define our data type personal record.

  14. 4:13

    We need to make it an instance of encryptable, so we say instance, encryptable personal record where text is the return type of the function encrypt. In the next line, we define encrypt, followed by a variable name, myRecord, to represent what will pass into this function.

  15. 4:32

    The function concatenates the name and the ID values of my record, and passes the concatenated string to the SHA256 function to encrypt it. So now our personal record data type knows how to encrypt its data when needed. Finally, in the script, we construct a personal record and bind it to some data as before, and then pass it to the encrypt function.

  16. 4:55

    This function returns text, which we bind to the encrypted data variable and then print it using the debug function. Notice how the statement binding encrypted data also has its data type as text defined in it? This is just a shorthand of defining a variables type and binding it to its value in one line. So let's go over what we've covered.

  17. 5:19

    Typeclasses represent a category of types. They specify a function that needs to be implemented by a type to become an instance of the typeclass. The most commonly used typeclasses are show, Eq and Ord. We can create our own data types and typeclasses and make the data types implemented the behavior of those typeclasses by implementing their functions.