Skip to content

Special values

Leon Starr edited this page Sep 4, 2021 · 4 revisions

Each domain may require certain special values like 32 degF, pi, 0.0 and so forth. You don’t necessarily want to devote specification attributes to these values since they don’t really describe your modeled subject matter. But if the values are not specific to your subject matter, they must be specific to something! You could argue, for example, that 32 degF is specific to the temperature data type. Pi is a mathematical constant. While pi is an irrational number, in mathematics, it is always rounded down in the computer world to some rational value. Zero is a special integer which may be implemented in a variety of implementation types (double, unsigned, etc).

In all of these cases we can make use of a special operation called a “selector” defined on the appropriate type.

Type selectors and type operations

A type defines a set of values that can be assigned and a selector chooses one such value for that type.

Let’s say that we want to select the number 2 from the system defined data type Integer. We could then define a selector operation named “2” invoked like this:

x = Integer(two) // Probably not a great idea

Here’s the trick, though. You have to go in and define that selector for the relevant data type before it will work. So you shouldn’t assume that a two selector has been defined unless you’ve added it yourself in the type definition.

For a more practical example, you could access pi like this:

pi = Real(pi)

For the temperature example:

t = Temperature(freezing h2o).DegF

Here we have a selector combined with a conversion operation. The selector is always parenthesized while the conversion operation is accessed via dot notation.

For zero, it depends on your data type:

d = Duration(immediate).Seconds // gives you zero seconds

If no value is supplied between the parentheses or if the parentheses are omitted, the default initial value defined for the type will be assigned. Given that it is zero for the base types Rational and Integer, you can do this:

rzero = Rational izero = Integer

This trick also works if you want the current time:

now = Date.HMS // current time is the default initial value

Note that the HMS operation is defined on the Date type and is applied to the default current time value. There is no need for a selector since we are assuming we get the default initial value, but we do specify the conversion operation to get the date in the desired format.

The same principle works with non-numeric values. Let’s say that you want to create all new instances of Folder with the string “Untitled”. Define that value as the initial default of the Folder Name type and you would do this:

newFolder name = Folder Name

Introduction

Model semantics

Flows (as Variables)

Constants and literals

Structure of an activity

Accessing the class model

Data flow


Grammar and parsing notes

Components

Clone this wiki locally