Just My Type(s)
As I've been explaining Toccata, I've used the core types to illustrate the various concepts. But now that I've cracked open the door on how Toccata uses type assertions to keep code correct, it's time to get a little crazy.
Yep, I'm gonna talk about making your very own types.
It's (almost) all in the definition
There are a couple of pieces of information you need to define a type; a name and names for each of the fields. In the simplest case, this looks like
This defines a type Flummer
that has 3 fields. So each Flummer value will actually be 3 values that are forever bound together. (Implementation detail: each Flummer value is actually a Vector. But you can't get access to it.) Those three values can be literally any valid Toccata value, including other Flummer values or Vectors or HashMaps or ...
And how are these Flummer values created? Glad you asked.
Couldn't be easier. The order the values are given in the call to the Flummer
function, called the construcor BTW, matches the order of the fields in type definition. So for this particular value the fields are assigned values like this
And if we have a Flummer value and want the value in it's phlux
field? We use the field accessor
evaluates to the value 'hook
.
There's one more trick here with fields. Given a Flummer value, we can create a new Flummer value by replacing a fields value using the a field accessor like so
This let
expression evaluates to a new Flummer value with fields
and the Flummer value flm
was automatically garbage collected because it was no longer used.
But why ...
Since you're reading this post, I'm assuming you're an elite programmer and don't need me to explain why you might want to define you're own types. OTOH, you might be wondering why the title is Product Types
. That because you can restrict what types of values can be assigned to the fields. So let's take a look at this totally hypothetical type
For this type, the tag
field must always be a string and the attributes
must be a HashMap. But the contents
field is wide open. It can be anything. So if you try to create an HTML value with a symbol as a tag rather than a string, it's going to blow up at compile time.
does this
And one more point for clarification
Different types can have the same names for fields. And those fields can be of different types even if the names are the same.
And why is this called a product type? This post has gone long enough, so I'm going to fob you off to someone else to answer that.