-
Notifications
You must be signed in to change notification settings - Fork 0
Standards
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.
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 |
For use in hash maps and other similar utilities, most objects should implement a no-arg hashCode
function which returns a bounded integer.
A value is considered truthy if it has an asBool
method and that method successfully returns true. Otherwise it is falsey.
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.
An iterator is an object with the functions next
and hasNext
, each with 0 required parameters.
An iterable is an object which is an iterator itself, OR an object with the function iterator
, which returns an iterator
A collection is an object which is both an iterable AND has the function contains
with 1 required parameter.
A pair is an object with the left
and right
function, each with 0 required parameters.
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.
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)
Home | New Issue | Contact Me