Files
wren/doc/receiver-less calls.txt

77 lines
1.9 KiB
Plaintext
Raw Permalink Normal View History

2013-11-13 17:09:55 -08:00
Q1: What does this resolve to:
foo(arg)
It could be:
2014-01-13 20:18:42 -05:00
1. this.foo(arg)
2. EnclosingClass.foo(arg) // i.e. a static method call
2013-11-13 17:09:55 -08:00
3. a call to a top-level function foo()
If we adopt the idea that a module is just a class definition (with some
syntactic differences) and classes can be nested, then 3 really means "a call
to a static method on the class surrounding the enclosing class".
I *don't* think we want the answer to the question to vary based on the name
in question. We can't rely on name resolution to disambiguate because we don't
know the full set of surrounding names in a single pass compiler. Also, it's
semantically squishier.
I think the right answer is 1, it's an implicit call on this. That's what you
want most often, I think. For imported modules, we could import them with a
"prefix" (really import them as objects bound to named variables), so calling
a top-level function in another module would be something like:
someModule.foo(arg)
This leaves the question of how *do* you call top level functions in your own
module? I.e., how do we call foo here:
def foo(arg) { IO.write("called foo!") }
2013-11-13 17:09:55 -08:00
class SomeClass {
bar {
// Want to call foo here...
}
}
This is analogous to:
class SomeModule {
static foo(arg) { IO.write("called foo!") }
2013-11-13 17:09:55 -08:00
class SomeClass {
bar {
// Want to call foo here...
}
}
}
The obvious solution is to use the class name:
class SomeModule {
2014-01-13 20:18:42 -05:00
static foo(arg) { IO.print("called foo!") }
2013-11-13 17:09:55 -08:00
class SomeClass {
bar {
SomeModule.foo(arg)
}
}
}
Which just leaves the question of what the class name of a top-level "module
class" is.
Idea: it's unnamed, so you just use a leading ".":
2014-01-13 20:18:42 -05:00
def foo(arg) { IO.print("called foo!") }
2013-11-13 17:09:55 -08:00
class SomeClass {
bar {
.foo(arg)
}
}
This mirrors C++'s unnamed scope thing:
::foo(arg);