User Tools

Site Tools


Sidebar

Passage markup

basics

coding

extra

list

section

whitespace

Macros

basics

colour

data structure

date and time

game state

live

maths

number

revision

saving

showing and hiding

string

styling

url

deprecated

Types of data

Special keywords

Special passage tags

harlowe:datatype

If you want to check if a variable's data is a certain type - for instance, that a variable that should only hold a number does indeed do so - then you can use these special values to do the comparison. To check if the data in $money is a number, write $money is a num.

Warning: you must write is a - don't write $money is num, because is by itself checks if the left side exactly equals the right side, and num represents all numbers, not the specific number contained in $money.

All of the datatypes are as follows.

Value Data type
number, num Numbers
string, str Strings
boolean Booleans
array Arrays
datamap, dm Datamaps
dataset, ds Datasets
command Commands
changer Changers
color, colour Colours

Note that data that can't be stored in a variable doesn't have a corresponding datatype name, because you won't need to compare things to it.

Additionally, along with the is a operator, there is a matches operator which is useful when you're dealing with data structures like arrays or datamaps. $pos is a array checks if $pos is an array, but that may not be precise enough for you. $pos matches (a: number, number) checks to see if $pos is an array containing only two numbers in a row. A data structure with datatype names in various positions inside it is called a pattern, and matches is used to compare data values and patterns.

Some more pattern-matching examples:

  • (datamap:'a',2,'b',4) matches (datamap:'b',num,'a',num)) is true.
  • (a: 2, 3, 4) matches (a: 2, num, num) is true. (Patterns can have exact values in them, which must be equal in the matching data).
  • (a: (a: 2), (a: 4)) matches (a: (a: num), (a: num)) is true.

To summarise, the two datatype-checking operators are:

Operator Purpose Example
matches Evaluates to boolean true if the data on the left matches the pattern on the right. (a:2,3) matches (a: num, num)
is a, is an Similar to matches, but requires the right side to be just a type name. (a:2,3) is an array, 4.1 is a number
harlowe/datatype.txt · Last modified: 2019/04/16 04:03 by l