You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We currently have filter-maps and functions, which are very similar, but have an important difference: only filter-maps can define values. This is because filter-maps have a define section where functions only have a block of expressions. This stems from older versions of Roto.
Instead of this:
filter-map main(x: u64) {
define {
y = 2 * x;
}
apply {
accept y
}
}
I want to allow this:
filter-map main(x: u64) {
let y = 2 * x;
accept y
}
Rules
This does open up some questions, mostly about scoping and mutability. Note that I'm not (yet) proposing to have a syntax for assignment. Here are the rules that I'd like to propose:
Variables can only be used in the lexical scope they are defined in.
Variables can only be used after their declaration.
Variables must be initialized at the declaration.
Variables cannot shadow variables in the same scope. They can shadow variables from outer scopes.
Variables are not (yet) mutable.
So these are not allowed:
if blabla {
let x = 3;
}
log(x); # breaks rule 1
log(x); # breaks rule 2
let x = 3;
let x; # breaks rule 3 (will also be invalid syntax)
x = 3;
let x = 3;
let x = x + 1; # breaks rule 4
let x = 3;
x = 2; # breaks rule 5 (will also be invalid syntax)
As an extension to this, we can introduce type annotations for let bindings.
I'm assuming that using a keyword for variable declaration is familiar to most users, because Perl, Lua, JavaScript and many other languages all have it.
Alternatives
Use a different syntax (e.g. x := 3 or x = 3). This is not great if we want to allow some mutability and muddles the scoping (case in point: Python).
Introduce some other way to add variables
Introduce a let ... in construct like functional languages
Use a C-like syntax with the type? (yuck)
Choose a different set of rules (allow shadowing? disallow all shadowing?)
The text was updated successfully, but these errors were encountered:
Introduction
We currently have
filter-map
s andfunctions
, which are very similar, but have an important difference: onlyfilter-map
s can define values. This is becausefilter-map
s have adefine
section wherefunction
s only have a block of expressions. This stems from older versions of Roto.Instead of this:
I want to allow this:
Rules
This does open up some questions, mostly about scoping and mutability. Note that I'm not (yet) proposing to have a syntax for assignment. Here are the rules that I'd like to propose:
So these are not allowed:
As an extension to this, we can introduce type annotations for
let
bindings.I'm assuming that using a keyword for variable declaration is familiar to most users, because Perl, Lua, JavaScript and many other languages all have it.
Alternatives
x := 3
orx = 3
). This is not great if we want to allow some mutability and muddles the scoping (case in point: Python).let ... in
construct like functional languagesThe text was updated successfully, but these errors were encountered: