Core

Object  Object

The "root" of everything. Everything delegates to Object, including toplevel objects.

Example:

> True is-a?: Object
True
> is-a?: Object
True

Generally, to create new objects you send clone to this if there's nothing more specific.

Example:

MyBoolean = Object clone
MyTrue = Boolean clone
MyFalse = Boolean clone
p := e  @ok

Defines e to be evaluated when the message described by p is dispatched to any of its targets. Returns @ok. See definition-syntax.

When defining a method of the same name as an existing one, it will be inserted with the most precise first. If two patterns are equivalent, the new method replaces the old.

Example:

> 0 fib := 1
@ok
> 1 fib := 1
@ok
> (n: Integer) fib := (n - 2) fib + (n - 1) fib
@ok
> 5 fib
8

This defines three fib methods on Integer. The order of the definitions doesn't matter; they are inserted in order of precision, so the methods that pattern-match on 0 and 1 are always chosen over the more general Integer match.

The method's expression e is evaluated with a new toplevel object containing the bindings from the pattern-match as methods, and delegating to the context of the method definition.

Example:

> x foo: [y, z] bar: _ := this
@ok
> 1 foo: [2, $3] bar: 4
<object (delegates to 1 object)>
  x := 1
  y := 2
  z := $3
> a = 2
2
> foo := [1, a]
@ok
> foo
10
> { a = 3; foo } call
10

Here we're using this to simply return the toplevel object that the expression is evaluated in.

p = v  v

Pattern-match p on v, inserting the bindings from the match into the current scope. Returns the value it's matching on (v). See pattern-syntax.

Example:

> a = 1
1
> [x, y] = [2, 3]
[2, 3]
> @(ok: foo) = @(ok: 4)
@(ok: 4)
> [a, x, y, foo]
[1, 2, 3, 10]

In addition to simple pattern-matching, = can also define methods. However, it always evaluates the right-hand side before insertion, and roles in a message pattern are all objects (not patterns), unlike its brethren :=.

Example:

> a = 1
1
> X = Object clone
<object (delegates to 1 object)>
> X b = a + 2
3
> X b
3
x set!: y  y

A macro for set!:to:; updates a slot to a given value. Will not add a new slot, as = would in nested scopes, and panics if the slot isn't found.

Example:

> a = 1
1
> { a set!: 2 } call
2
> a
2
> b set!: @uh-oh
ERROR: <error @(undefined: "b")>
x set!: name to: value  value
  | name is-a?: String

Update slot name on x to value, searching for where the slot is defined (following delegates) to ensure it is updated rather than overridden. If the slot is not found, it panics.

Example:

> a = 1
1
> { set!: "a" to: 2 } call
2
> a
2
> set!: "b" to: 2
ERROR: <error @(undefined: "b")>
o clone  is-a?: o

Returns a new object, delegating to o.

o copy  any

Copies object o, creating a new object with the same methods and delegates.

o new  is-a?: o

Generally, this initializes a new clone of o, which will usually be the base object (e.g. Regexp itself). The initialization typically uses clone and sets up some state.

This is not defined for Object; it (or some variation) should be defined for any object's initialization rather than overriding clone.

Other conventions are new: and new.foo: _ bar: _, with any additional optionals.

o id  o

Trivial method that returns its target.

Example:

> 1 id
1
> @id id
@id
> @id call: @ok
@ok
x delegating-to: y  @ok

Yields a new object with x's method table and y affixed to its delegates list.

x with-delegates: ds  @ok
  | ds is-a?: List

Yields a new object with x's method table and ds as its delegates list.

x delegates-to?: y  Boolean

Returns True if x delegates to y.

Example:

> X = Object clone
<object (delegates to 1 object)>
> Y = Object clone
<object (delegates to 1 object)>
> Y delegates-to?: X
False
> Y (delegating-to: X) delegates-to?: X
True
x delegates  List

Returns a list of x's delegates.

Example:

> x = 1 clone
<object (delegates to 1 object)>
> x delegates
[1]
> x (delegating-to: 2) delegates
[1, 2]
x super  any

Returns the first value that x delegates to.

Example:

> 1 clone super
1
> 1 clone (delegating-to: 2) super
1
x is-a?: y  Boolean

Returns True if x is equal to y or delegates to it, directly or indirectly.

Example:

> 1 is-a?: Integer
True
> X = Object clone
<object (delegates to 1 object)>
> Y = Object clone
<object (delegates to 1 object)>
> X is-a?: X
True
> Y is-a?: X
False
> Y (delegating-to: X) is-a?: X
True
x responds-to?: p  Boolean
  | p is-a?: Particle

Check if object x would respond to the partial message p.

The particle can have any number of roles missing; the first slot is filled and a "partial match" is done. That is, when searching for a method, only the filled-in roles are matched with; empty slots are a match for any role in a method's message pattern.

Example:

> X = Object clone
<object (delegates to 1 object)>
> X responds-to?: @foo
False
> X foo = 42
42
> X responds-to?: @foo
True
> 1 responds-to?: @+
True
> 1 responds-to?: @(+ 2)
False
> 1 responds-to?: @(+ "foo")
False
x has-slot?: name  Boolean
  | name is-a?: String

Check if object x directly (that is, not through delegates) contains a slot called name.

Implies x replies-to?: (Particle new: name).

Example:

> x = Object clone
<object (delegates to 1 object)>
> x a = 1
1
> x has-slot?: "a"
True
> x clone has-slot?: "a"
False
x show  String

Convert x to a proper representation of that value in code (if possible).

Example:

> 10 show
"10"
> "foo" show
"\"foo\""
> False show
"False"
top load: filename  @ok
  | filename is-a?: String

Executes the file filename with top as its top scope.

top require: filename  @ok
  | filename is-a?: String

Like load:, but loaded files are remembered (by their canonicalized, absolute path), so subsequent attempts to require them will be a no-op, returning @ok.

v match: branches  any
  | branches is-a?: Block

Pattern-match v on the patterns described by branches, evaluating the first branch that matches the value.

Errors with @(no-match-for: v) if none of the branches match the value.

Example:

> 1 match: { 2 -> @nope; 1 -> @yep }
@yep
> 1 match: { 2 -> @nope }
ERROR: <error @(no-match-for: 1)>
> [1, 2] match: { [1, b] -> @(got: b) }
@(got: 2)
v case-of: branches  any
  | branches is-a?: Block

Similar to match:, but performs some case-specific matching via matches?:. For example, a Range matches if a value is within the range, and a Regexp test will perform the match on the string.

Using _ as the test acts as a catch-all.

Yields @ok if none of the branches match the value.

Example:

> 1 case-of: { 1 -> $1; 2 -> $2 }
$1
> 2 case-of: { 1 -> $1; 2 -> $2 }
$2
> 1 case-of: { 2 -> $2 }
@ok
> 1 case-of: { 2 -> $2; _ -> @unknown }
@unknown
> 5 case-of: { (0 .. 4) -> "0-4"; (0 .. 10) -> "0-10" }
"0-10"