Learn SugarBeginner

Values and types

Understand the kinds of information Sugar can represent and how a type describes a value.

Every piece of information in Sugar is a value. A value might be a name, a score, an answer to a yes-or-no question, a date, an Item, or a collection of other values.

A type describes what kind of value something is. The type determines which operations make sense. Sugar can add two numbers, for example, but it cannot subtract one Item from another.

Scalar values

Sugar has three scalar types. A scalar holds one direct value:

ValueTypeMeaning
"Midnight"strA string: characters kept in order
42numberA number
trueboolA yes-or-no value

Scalar type names are lowercase.

Quotation marks matter. "health" is a string containing the word health. health without quotation marks is a name that Sugar tries to look up.

A boolean has only two possible values:

true
false

Compound values

Some values carry related information or contain other values. These are compound values.

Sugar writes compound type names in PascalCase:

TypeValue it represents
ItemAn Item selected from inventory
DateOne date and time
DurationAn amount of elapsed time
JoiOne JOI identity
SeatOne participant and their host Item inside a Session
MediaAn image or video resource
FnA first-class closure that can be called

You do not create all of these values in the same way. Later lessons introduce each type when it becomes useful. For now, the important distinction is that a number holds one scalar value, while an Item is a compound value with properties such as its name and remaining uses.

Lists have an entry type

A list keeps zero or more values in order. The values inside it are called its entries:

("north", "east", "below")

This is a list of strings. The Lists lesson teaches how to create, read, and change lists.

When Sugar needs an explicit list type, it writes the entry type between angle brackets:

List<number>
List<str>
List<bool>
List<Joi>

List describes the container; the name inside < and > describes every entry allowed inside it and is called a type argument. Every Sugar value type can be used, including another list type.

For example, read List<Joi> as “list of JOIs,” List<number> as “list of numbers,” and List<List<bool>> as “list of lists of booleans.”

None means no value

None represents the absence of a value:

None

It is different from 0, false, and "". Those are present values: the number zero, the boolean false, and an empty string.

Only declarations and operations that explicitly allow an absent value may use None. For example, an optional Item input begins as None because no Item has been selected yet.

The next lesson gives values names by declaring variables. It also shows where type names appear in real Sugar code.