Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

let bindings #86

Open
tertsdiepraam opened this issue Dec 19, 2024 · 0 comments
Open

let bindings #86

tertsdiepraam opened this issue Dec 19, 2024 · 0 comments

Comments

@tertsdiepraam
Copy link
Contributor

tertsdiepraam commented Dec 19, 2024

Introduction

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:

  1. Variables can only be used in the lexical scope they are defined in.
  2. Variables can only be used after their declaration.
  3. Variables must be initialized at the declaration.
  4. Variables cannot shadow variables in the same scope. They can shadow variables from outer scopes.
  5. 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?)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant