Comments (7)
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.
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.
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.
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.
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.
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.
Yes, the idea is that you can indeed close over immutable variables in function scopes.
from virgil.
Related Issues (20)
- Value of `null`? HOT 10
- .NET CLR target HOT 1
- Add Windows batch scripts for JAR and WASM target HOT 1
- Supporting `unsafe` as a keyword? HOT 2
- Mutability of `def var` fields in subclasses HOT 3
- Support socket flags/options
- Improve type system and type inference rules HOT 4
- Change system and pointer APIs to pervasively use the `Range` type HOT 2
- Copy/modify initialization expression - `with`?
- Weakening `private` to file scope HOT 4
- Shard testsuite and parallelize
- Support Windows 64 bit
- Most of documentation files seem to be empty HOT 1
- CI Scripts don't report failure properly
- make fails in the virgil folder HOT 6
- Git conflict markers in tests HOT 4
- Exclude code from compilation HOT 2
- Array type init with a negative value. HOT 4
- macos x86_64 11.7.10 build failed HOT 3
- Online documentation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from virgil.