Giter Club home page Giter Club logo

Comments (7)

titzer avatar titzer commented on May 18, 2024

Yeah. Internally I decided to call the feature funcexpr (function expressions) and the AST node FuncExpr.

I added a bunch of tests to test/funcexpr and I added parser support, but I disabled that on purpose until I do a stable revision (i.e. check in the results of aeneas bootstrap as the next stable binary), because I don't want stable to support incomplete features.

Personally I kind of like the fat arrow => from JavaScript lambda syntax. I went down that route but it requires backtracking to reinterpret expressions when the => is hit. I wrestled with some alternative syntaxes before eventually just settling on using the def keyword to introduce a function expression.

//@execute = 112
def main() -> int {
	return (def () => 112)();
}

I'd like to further expand the => operator to allow it to denote that a method has a body that consists of a single expression and its return type is implicitly that expression's type.

@execute 33=33
class C(val: int) {
  def inc() => val++;
}
def main(a: int) => C.new(a).inc();

from virgil.

srackham avatar srackham commented on May 18, 2024

What is the reason for a separate lambda syntax, why not just have anonymous nested methods? This is the route taken by Go, it's more general and there's one less concept e.g. https://gobyexample.com/closures

from virgil.

titzer avatar titzer commented on May 18, 2024

Actually I think what I am proposing is really close to what Go does.

The fat arrow => is just a shorthand that is actually independent of whether you're declaring a method or creating a closure with an anonymous function (function expression).

E.g. a method:

def foo() => 0;
// is equivalent to:
def foo() -> int {
  return 0;
}

And a function expression:

var x = def() => 0;
// is equivalent to:
var x = def() -> int { return 0; }

Have a look at some of the examples in test/funcexpr, I think it will be clearer.

from virgil.

srackham avatar srackham commented on May 18, 2024

Have a look at some of the examples in test/funcexpr, I think it will be clearer.

Much clearer, thanks.

The test examples are all single-statement anonymous functions. Are multi-statement anonymous functions allowed? For example is this closure over a multi-statement anonymous function valid?

def intSeq() -> def() -> int {
    var i = 0;
    return def() -> int {
        i = i + 1;
        return i;
    }
}

from virgil.

titzer avatar titzer commented on May 18, 2024

Yes, that would be valid (in terms of syntax[1]), though I think I want to disallow closing over mutable locals and only allow closing over def locals, loop variables, and assigned-once variables.

[1] with a minor correction in the return type:

def intSeq() -> () -> int {
    var i = 0;
    return def() -> int {
        i = i + 1;
        return i;
    }
}

Java also doesn't allow closing over mutable locals. So you could, e.g. use an array.

def intSeq() -> () -> int {
    def v = [0];
    return def() -> int {
        return v[0]++;
    }
}
// or, shorter
def intSeq() -> () -> int {
    def v = [0];
    return def() => v[0]++;
}

from virgil.

diakopter avatar diakopter commented on May 18, 2024

as long as they allow closing over something in outer scopes, Virgil would be able to idiomatically express Knuth's so-called "Man or Boy test" as depicted here for other programming languages - https://rosettacode.org/wiki/Man_or_boy_test

(idiomatically, as opposed to simulating such lambda-closures with classes)

from virgil.

titzer avatar titzer commented on May 18, 2024

Yes, the idea is that you can indeed close over immutable variables in function scopes.

from virgil.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.