Skip to content
This repository has been archived by the owner on Mar 10, 2022. It is now read-only.

EEP 2 - lazy evaluation #6

Open
bristermitten opened this issue Sep 20, 2020 · 0 comments
Open

EEP 2 - lazy evaluation #6

bristermitten opened this issue Sep 20, 2020 · 0 comments
Labels
enhancement New feature or request

Comments

@bristermitten
Copy link
Member

bristermitten commented Sep 20, 2020

Elara Enhancement Proposal

Why does this Proposal exist?

Laziness is a key part of functional programming. Take Haskell for example: everything is lazy - no expression is evaluated until actually necessary.

Some languages implement this as part of their standard library - eg Kotlin or Rust (I think), however this proposal targets the language level.

What does this proposal not change?

  • The default evaluation strategy of expressions (eagerly) is not changed.

What does this Proposal suggest?

The proposal suggests a new keyword: lazy

The lazy keyword is applicable to:

  • Variable Declaration
  • Parameter Declaration

It should be placed before mut, and before the type in a parameter.

For example:

let lazy name = "Hello"

let some-function = (lazy Int value) => {
    //Something
    value + 1
}

What does this keyword do?

The lazy keyword delays the evaluation of a given expression until the identifier associated with that expression is referenced.

For example:

let and = (Boolean left, lazy Boolean right) => Boolean {
    if !left {
        return false
     }
    return right
}

some-bool and other.something()

If some-bool evaluates to false, other.something() is never evaluated as the function has already stopped execution.

In another example:

let lazy error-msg = print "Error"
print "Hello"
if condition {
    error-msg
}

The output of this program (assuming condition to be true) is

Hello
Error

Because the value of error-msg is not evaluated until it's actually referenced.

What does this Proposal achieve?

This primarily allows for some powerful optimisations in functions:

  • List.filter can avoid evaluating its predicate if the list is empty
  • Slow functions can avoid performing intensive calculations until necessary
  • Things like Boolean AND and OR could also make use of this for clever optimisations.

Laziness is also a useful tool for day to day programming, but again a language level feature seems more flexible than Lazy<T> or by lazy {}

@Vshnv Vshnv added the enhancement New feature or request label Sep 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants