Skip to content

Special values

Leon Starr edited this page Nov 6, 2023 · 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[2]

Scrall requires the square brackets to indicate a selector operation is being invoked.

Here’s the trick, though. You have to go in and define that 2 selector for the relevant data type before it will work. So you can't just assume that you can ask for any integer value.

You can define a selector to take a symbolic value like this:

pi = Rational[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 square braces or if they 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

The HMS operation is defined on the Date scalar 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