Skip to content

Instantly share code, notes, and snippets.

@DavidArno
Last active December 12, 2015 04:58
Show Gist options
  • Save DavidArno/4718376 to your computer and use it in GitHub Desktop.
Save DavidArno/4718376 to your computer and use it in GitHub Desktop.
In Lapsang static composition can be used to add features to types in a way that can be validated by the compiler.
The syntax for a new function component takes the form:
package some.component.package.name
component returnType functionName(type someName) default implFunctionName
component returnType implFunctionName(type someName)
code goes here
As an example, let's imagine we want to add a new method to all functions, called nameOfFirstParameter,
which returns the name of the first parameter, or "None" if it has none. The code might look something like:
package my.silly.component
component string nameOfFirstParameter(function this) self default
return "None" if this%parameters.length == 0 else this%parameters[0].name
The consequence of this is that, as long as my.silly.component package is exposed to the application then for
every function (including nameOfFirstParameter [I think]) can expose its first parameter name via code such as
var itsName = someMethod.nameOfFirstParameter()
To explain the "%", it needs to be realised that - because their is no concept of a base class - anything
that needs to be universally applied across the language is done so via components. Thus there is a component
Metadata built in to the language core libraries. Because x.Metadata.yyy will be such a common expression, the
syntactic sugar version of x%yyy is also provided.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment