3.1 Global Utilities
3.1.1 Built-in Utility Functions
Displays the provided value after first calling to-repr on it, then returns the value.
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>".
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
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.
check: # tostring does not wrap strings in quotes tostring("Hello, world.") is "Hello, world." to-string("Hi.") == "Hi." is true end
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
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.
check: [list: nothing, nothing, nothing].length() is 3 end
3.1.3 Type Predicates
A number of functions are available to tell which kind of builtin value a particular value is.
Returns true if the provided argument is a boolean, false if not.
Returns true if the provided argument is a Boolean, false if not.
check: is-boolean(true) is true is-boolean(false) is true is-boolean(0) is false end
Returns true if the provided argument is a number, false if not.
Returns true if the provided argument is a Number, false if not.
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
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
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.
check: is-string("Hello, world!") is true is-string(```Multi line string```) is true end
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.
check: is-raw-array([raw-array: 3, "Jones", false]) is true end
Returns true if the provided argument is nothing, false if not.
Returns true if the provided argument is a Nothing, false if not.
check: is-nothing(nothing) is true is-nothing(0) is false is-nothing(empty) is false is-nothing("") is false end
Returns true if the provided argument is a function, false if not.
Returns true if the provided argument is a Function, false if not.
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
Returns true if the provided argument is an object, false if not.
Returns true if the provided argument is a Object, false if not.
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