Skip to content

Standards

Socratic_Phoenix edited this page Aug 21, 2017 · 9 revisions

<-- Back | Next -->


Due to the dynamic classless nature of Shnap, it is necessary to have standard fields that contractually do/are certain things. Below is a list of these fields.

Conversion Functions

Function Args Purpose
asNumber none Converts the given object to a native number
asString none Converts the given object to a native string
asBoolean none Converts the given object to a native boolean
asArray none Converts the given object to a native array
asJava none Converts the given object to a java type

Hash Codes

For use in hash maps and other similar utilities, most objects should implement a no-arg hashCode function which returns a bounded integer.

Truthiness

A value is considered truthy if it has an asBool method and that method successfully returns true. Otherwise it is falsey.

Errors

Hierarchical errors can be incredibly useful, but Shnap is classless, and thus does not have a type hierarchy. Furthermore, since Shnap is dynamic and classless, any object can be thrown. Therefore, by standard, only objects created from the builtin error function (or its derivatives) should be used for throwing. See builtin errors for more information.

Iterator

An iterator is an object with the functions next and hasNext, each with 0 required parameters.

Iterable

An iterable is an object which is an iterator itself, OR an object with the function iterator, which returns an iterator

Collection

A collection is an object which is both an iterable AND has the function contains with 1 required parameter.

Pair

A pair is an object with the left and right function, each with 0 required parameters.

Map

A map is an object with the set, get, and keys functions, and which is iterable over pairs. set must have 2 required parameters, get must have 1, and keys must have 0.

Operators

Any type can freely override operators. See the list of operators for the appropriate function names. However, there are some special cases for binary operators. Whenever a binary operator is invoked, both the left and right arguments are checked for the appropriate function. Of course, this means that sometimes, right.add(left) is called. If the add method is not commutative, this may cause problems! To solve this, operator functions are provided with the field order, which is 1 if the order is right.function(left), or 2 if the order is left.function(right). Therefore, a sample implementation of add might be:

add = $(arg,order=1) {
    if (order == 1) {
        return: arg + internal
    } else {
        return: internal + arg
    }
}

Note that order has a default value. This is because binary operator functions must have one required parameter (the argument), and one default parameter (order)

Clone this wiki locally