Functions - brombres/Rogue GitHub Wiki
Overview
Rogue functions are function objects (AKA functors) that can be passed and stored like other objects and invoked like routines.
Function Type
Syntax
Function(ParamType1,ParamType2,...)->ReturnType
(Function(ParamType1,ParamType2,...)->ReturnType)
Description
- A Function Type is the type of a variable that can store a function.
- Parens can be added around an entire Function Type definition to avoid rare ambiguous situations.
Function
Syntax
# Single line specialized function.
function(param1:Type1,param2:Type2)->ReturnType: single; line; commands
# Multi-line specialized function.
function(...)->ReturnType
multi
line
commands
endFunction
# Single-line generic function (must be used as an arg)
(param1,param2) => single; line; commands
# No-params generic function
() => single; line; commands
# Shorthand for no-params generic function (can omit leading '()')
=> single; line; commands
# Specialized function with explicit value capture.
function(...)->ReturnType with (a,b=x,c=3,THIS=this) ...
# Generic function with explicit value capture.
local x = 3
(params...) with (x) => println(x)
# Generic function with implicit value capture.
local x = 3
(params...) => println(x)
# Implicit Single Arg Function (turns expression into a generic function)
$.name == "Alpha"
# Equivalent to: (arg) => arg.name == "Alpha"
# Implicit Multi-Arg Function (turns expression into generic function)
$1 < $2
# Equivalent to: (arg1,arg2) => arg1 < arg2
# Callback function that calls `obj.m(...)` when invoked
obj=>m
# Callback function that holds a weak reference to 'obj'; calling the
# callback will do nothing if the object has been GC'd.
obj?=>m
# Global method callback
SomeType=>m
# Routine callback
Routine=>routine_name # 'Routine' is literal; not a placeholder
Description
- Functions can be defined on the fly as part of larger expressions.
- Specialized functions are
instanceOfthe corresponding Function Type. For example,function(n:Int32)->Int32: return n*2isinstanceOftypeFunction(Int32)->Int32. - Generic functions must be stored in a variable with a specialized Function Type - either passed as an argument or assigned to a property or local. The parameter types and return type of a generic function is inferred at compile time.
- When a generic function is converted into a specialized function with a return type, if the final statement is a value that is not explicitly returned then an implicit
returnis added. - Defining a function-with-values (that has a
withclause) creates a new function object each time code containing the function definition is executed. - Functions that do not capture variables (using
with) use a singleton class mechanism and so do not create new objects as code containing the function definition is executed.
Example
class Name( first:String, last:String )
METHODS
method to->String
# Long form
local fn_default = function(first:String,last:String)->String
return "$ $"(first,last)
endFunction
return this->String( fn_default )
# Short form
# return this->String( (f,l)=>"$ $"(f,l) )
method to->String( fn:Function(String,String)->String )
return fn(first,last)
endClass
local name = Name("Brom", "Bresenham")
println name
# Brom Bresenham
println name->String( (f,l)=>"$, $"(l,f) )
# Bresenham, Brom