On this page:
3.1.1 Built-in Utility Functions
print
torepr
to-repr
tostring
to-string
raise
3.1.2 Built-in Types
Any
Boolean
Number
String
Raw  Array
Nothing
Function
Method
Object
Table
3.1.3 Type Predicates
is-boolean
is-number
is-string
is-raw-array
is-nothing
is-function
is-object

3.1 Global Utilities

3.1.1 Built-in Utility Functions

print :: (val :: a) -> a

Displays the provided value after first calling to-repr on it, then returns the value.

torepr :: (val :: Any) -> String

to-repr :: (val :: Any) -> String

Creates a string representation of the value that resembles an expression that could be used to construct it.

The to-repr of a string yields a string containing the Pyret syntax needed to write the original value as string literal: most characters are unchanged, but quotes, newlines, tabs, and backslashes are all escaped, and the whole value surrounded by quotes.

Functions are simply represented as "<function>".

Examples:

check: to-repr([list: 3, 5, 9]) is "[list: 3, 5, 9]" torepr("Hello, world.") is "\"Hello, world.\"" to-repr("Hi.") == "Hi." is false to-repr((lam(i): i + 1 end)) is "<function>" end

tostring :: (val :: Any) -> String

to-string :: (val :: Any) -> String

Creates a string representation of the value for display that is value-dependent in some cases, such as error messages. For built-in types the output is identical to torepr, except for Strings.

Examples:

check: # tostring does not wrap strings in quotes tostring("Hello, world.") is "Hello, world." to-string("Hi.") == "Hi." is true end

raise :: (val :: Any) -> Nothing

Raises the value as an error. This usually stops the program and reports the raised value, but errors can be caught and checked in tests by raises and by check: blocks.

3.1.2 Built-in Types

A type specification that permits all values. This is mainly useful in built-in language forms, like in equality or torepr, which truly do handle any value.

Pyret programs that use Any on their own can usually be restructured to use a specific type declaration to be more clear about what data they are working with.

Specifying Any will prevent Pyret from attempting to infer types, as it will if no type specification is provided.

The type of Booleans.

The type of Numbers.

The type of Strings.

The type of RawArray.

The type of the special value nothing, used in contexts where the program evaluates but has no meaningful answer by design (see, for example each). Note that nothing is still a value.

Examples:

check: [list: nothing, nothing, nothing].length() is 3 end

The type of all function values.

The type of all method values.

The type of all values constructed from data constructors and singletons, and by object literals.

The type of Tables.

3.1.3 Type Predicates

A number of functions are available to tell which kind of builtin value a particular value is.

is-boolean :: (val :: Any)

Returns true if the provided argument is a boolean, false if not.

Returns true if the provided argument is a Boolean, false if not.

Examples:

check: is-boolean(true) is true is-boolean(false) is true is-boolean(0) is false end

is-number :: (val :: Any)

Returns true if the provided argument is a number, false if not.

Returns true if the provided argument is a Number, false if not.

Numbers are
  • Integers, e.g. 345 or -321

  • Rationals, e.g. 355/113 or -321/6789

  • Inexact numbers, e.g. 123.4567 or -0.987

  • Complex numbers, e.g. 1+2i, where the real and imaginary components may be integers, rationals or inexact numbers

Examples:

check: is-number(-42) is true is-number(~6.022e+23) is true is-number(num-sqrt(2)) is true is-number("4") is false end

is-string :: (val :: Any)

Returns true if the provided argument is a string, false if not.

Returns true if the provided argument is a String, false if not.

Strings can be written "text" or 'text', and may not span multiple lines. Allowed escapes are \n (newline), \r (carriage return), \t (tab), \[0-8]{1,3} for octal escapes, \x[0-9a-fA-F]{1,2} for single-byte hexadecimal escapes, or \u[0-9a-fA-F]{1,4} for double-byte Unicode escapes. Additionally, \" escapes a double-quote within a double-quoted string, and \' escapes a single quote within a single-quoted string.

Multi-line string literals may be written ``` text ```. The same escape sequences are valid as for single-line strings. Leading and trailing whitespace of the string are trimmed.

Examples:

check: is-string("Hello, world!") is true is-string(```Multi line string```) is true end

is-raw-array :: (val :: Any)

Returns true if the provided argument is a raw array, false if not.

Returns true if the provided argument is a RawArray, false if not.

Examples:

check: is-raw-array([raw-array: 3, "Jones", false]) is true end

is-nothing :: (val :: Any)

Returns true if the provided argument is nothing, false if not.

Returns true if the provided argument is a Nothing, false if not.

Examples:

check: is-nothing(nothing) is true is-nothing(0) is false is-nothing(empty) is false is-nothing("") is false end

is-function :: (val :: Any)

Returns true if the provided argument is a function, false if not.

Returns true if the provided argument is a Function, false if not.

Examples:

fun inc(x): x + 1 end check: is-function(inc) is true is-function((lam(i): i + 1 end)) is true is-function({(y :: Number) -> Number: y + 1}) is true is-function(method(self): self + 1 end) is false end

is-object :: (val :: Any)

Returns true if the provided argument is an object, false if not.

Returns true if the provided argument is a Object, false if not.

Examples:

data Point: pt(x, y) end check: is-object(pt(3, 4)) is true is-object({x : 12, y : 7}) is true is-object({(y :: Number) -> Number: y + 1}) is false end