Default values for functions
By default, Keli does not supports default values for function parameters. However, this can be emulated using objects and lambda's shorthand. Not only that, it will be much better than the default values in other languages such as Python and TypeScript in terms of code completion supports.
The following demonstrates how to create a graph library with a plot function that takes default values.
1
// Defining structure of a point
2
Point = $.x(Float) y(Float)
3
4
5
// Defining default graph config
6
GraphType = choice.Line.Scatter.Bar
7
GraphConfig = $.
8
type(GraphType)
9
xAxisLabel(String)
10
yAxisLabel(String)
11
title(String)
12
13
// defining default graph config
14
defaultGraphConfig = GraphConfig.
15
type(GraphType.Line)
16
xAxisLabel("x")
17
yAxisLabel("y")
18
title("title")
19
20
// defining the plot function
21
(this Array.of(Point)).plot(f Function.in(GraphConfig) out(GraphConfig)) =
22
this._plot(f.apply(defaultGraphConfig)) // the magic happens here
23
24
// defining the plot function helper
25
(this Array.of(Point))._plot(config GraphConfig) = undefined
26
27
// usage
28
points = [$.x(0) y(0), $.x(1) y(1), $.x(2) y(2)]
29
points.plot(.title("My graph").xAxisLabel(""))
Copied!
Last modified 2yr ago
Copy link