fastnthere is a concept of
or-typecan be used when give you a way of saying a value is one of a possible set of values. Consider we are defining shapes, and a shape can be either a rectangle, a circle or a triangle.
-- or-type shape: -- record rectangle: decimal width: decimal height: -- record triangle: decimal ab: decimal bc: decimal ca: -- record circle: decimal radius: -- end: or-type This type is loosely equivalent to Rust's enum and is also known as an [algebraic data type](https://en.wikipedia.org/wiki/Algebraic_data_type).
or-typetype is declared using the
or-typekeyword followed by the name of the type. The syntax for the
or-typedeclaration is as follows:
-- or-type worker: ;; Anonymous Record -- record individual: caption name: string designation: ;; Regular variant, defined using existing type, here we have used `string` -- string ceo: ;; Constant -- constant string bot: BOT -- end: worker
To understand the
or-type, let's consider an example of a sales business that
wants to get "leads". A lead can either be an individual or a company, where
individuals have fields like their name and phone number, and companies have
fields like company name, name of contact, and fax number.
or-type, we can use the following syntax:
-- or-type lead: -- record individual: caption name: string phone: -- record company: caption name: string contact: string fax: -- end: lead
Here, we used ftd::p1's "sub-section" to represent each possibility.The declarations
or-typevariants, and they use similar syntax as
recorddeclarations. These type of variant is called
or-typevariants are of three types:
Anonymous Record variant declares a record with fields, similar to a record
declaration. However, the fields are defined directly within the
declaration. It is called
anonymous because there is no pre-defined
type that exists for this variant.
individualvariant in the
or-typedeclaration is an Anonymous Record variant:
-- record individual: caption name: string phone:
individual variant has no predefined type, but a record is created on the
spot, which becomes the type for the
-- lead.individual john: John Doe phone: 9999999999 -- lead.company my-company: My Company contact: 9999999999 fax: 7368632
johnis of variant
my-companyis of variant
company. We then provide values for their respective fields.
Regular variant declares any defined type and expects the value provided of
that type. It uses a similar syntax to a variable declaration, where we specify
the name of the variant and the expected data type.
-- or-type length: -- integer px: -- decimal percent: -- end: length
Here, both variants,
percent, are of regular type. i.e. They expect
values of the provided type when declaring a variable, field, or component
-- length.px pixel-length: 100 -- length.percent percent-length: 10
pixel-lengthis of variant
pxthat accepts an
integertype value, and
percent-lengthis of variant
percentthat accepts a
Constant variant is similar to a
Regular variant, but it expects a
constant value rather than a variable value. We use the
constant keyword to
define this variant.
-- or-type weekday: -- constant string sunday: Sunday -- constant string monday: Monday -- constant string tuesday: Tuesday -- constant string wednesday: Wednesday -- constant string thursday: Thursday -- constant string friday: Friday -- constant string saturday: Saturday -- end: weekday
In this example, we declare an
or-type called weekdays with seven variants.
Each variant is a
Constant of type
string, with a fixed value.
-- weekday today: monday
or-type is a way to create an enumeration of variants in
programming. It allows you to define a list of possible variants, each with its
own set of fields, and then use those variants in your code.
can be of three types: Anonymous Record, Regular, and Constant.
or-typein situations where you need to choose a value from a set of predefined variants. For example, when working with data that has multiple possible formats or when you need to define a set of constants for your application.
Some benefits of using
Clear and concise code:
or-type allows you to define a set of variants
in a single place, making your code more organized and easier to read.
Type safety: By defining the possible variants upfront, you can ensure that your code only accepts values of the correct type, reducing the risk of runtime errors.
or-typevariants can have their own set of fields, which allows you to define complex data structures with ease.