Promise: Method slots and operators
Before getting into method slots, here's a quick review of the Promise lambda grammar:
lambda: [<aignature>] <expression>;
signature: (arg1>, ... <argN>[|<return-type>])
arg: [<type>] <argName>[=<init-expression>]
expression: <statement> | { <statement1>; ... <statementN>; }
A lambda can be called with positional arguments either with the parentheses-comma convention ( foo(x,y)
) or the space-separated convention ( foo x y
), or with a JSON object as argument ( foo{ bar: x, baz: y}
).
Method Overload (revised)
When i decided to use slots that you assign lambdas as methods, I thought I'd be clever and make those slots polymorphic to get around shortcomings i perceived in the javascript model of just attaching functions to named fields. After listening to Rob Pike talk about Go at OSCON, I decided this bit of cleverness did not serve a useful purpose. In Go there are no overloads, because a different signature denotes different behavior and the method name should reflect that difference. Besides, even if you want overload type behavior in Promise, you can get it via the JSON calling convention:
class Index {
Search:(|SearchResult) {
foreach(var keyvaluepair in $_) {
// handle undeclared named parameters
}
...
};
}
Basically the lambda signature is used to declare an explicit call contract, but using a JSON object argument, undeclared parameters can just as easily be passed in.
If a method is called with positional arguments instead of a JSON object, the default JSON object will contain a field called args with an array value
class Index {
Search: {
...
};
}
Index.Search('foo','documents',10);
// $_ => { args: ['foo','documents',10] }
The above signature shows a method assigned a lambda without any signature, i.e. it accepts any input and returns an untyped object. Receiving $_.args
is not contingent on that signature, it will always be populated, regardless of the lambda signature.
Wildcard Method
A class can also contain a wildcard method to catch all method calls that don't have an assigned slot.
class Index
*: {
var (searchType) = $_._methodname./^Find_(.\*)$/;
if(searchType.IsNil) {
throw new MethodMissingException();
}
...
};
}
The wild card method is a slot named *
. Retrieving the call arguments is the same as with any other method without declared signature, i.e. $_
is used. In addition, the methodname used in the call is stuffed into $_
as the field _methodname
.
The above example shows a method that accepts and call that starts with Find_
and takes the remainder of the name as the document type to find, such as Find_Images
, Find_Pages
, etc. This is done by using the built in regex syntax, i.e. you can use ./<regex>/
and ./<regex>/<substitution>/
on any string (or the string an object converts to), similar to perl's m//
and s///
. Like perl, the call returns a list of captures, so using var
with a list of fields, in this case one field called searchType
, receives the captures, if there is a match.
When a method is called that cannot be found on the Type, it throws a MethodMissingException
. A wildcard method is simply a hook that catches that exception. By throwing it ourselves, our wildcard reverts to the default behavior for any method that doesn't match the desired pattern. This also gives parent classes or mix-ins the opportunity to fire their own wildcard methods.
Wildcard methods can only declared in classes and mix-ins, not on Types. Types are supposed to be concrete contracts. The existence of a wildcard does mean that the class can satisfy any Type contract and can be used to dynamically implement type contracts without having to declare each method (think mocks).
Operators
Operators are really just methods called with the whitespace list syntax
var x = 3;
var y = x + 5; // => 8
var z = x.+(5); // => 8
// most operators implements polish notation as appropriate
var v = x.+(5,6); // => 14
Operators are just methods, which means you can assign them yourselves as well
class Query {
List<Query> _compound;
op+: {
var q = Query();
q._compound.AddRange(_compound);
q._compound.AddRange($_args);
q;
};
}
The only difference between a normal method slot and an operator slot is that the operator slot has the op
prefix for disambiguation.
And now for the hard part
That concludes the overview of the things I think make Promise unique. There's certainly tons more to define for a functioning language, but most of that is going to be very much common syntax. So now it's time to buckle down dig into antlr and the DLR to see what it will take to get some semblance of Promise functioning.
More about Promise
This is a post in an ongoing series of posts about designing a language. It may stay theoretical, it may become a prototype in implementation or it might become a full language. You can get a list of all posts about Promise, via the Promise category link at the top.