On Sun, May 1, 2016 at 4:57 AM, Tamas Papp <[email protected]> wrote:
>
> On Fri, Apr 29 2016, Yichao Yu wrote:
>
>> On Fri, Apr 29, 2016 at 8:33 AM, Tamas Papp <[email protected]> wrote:
>>> Default value expressions for optional and keywords arguments are
>>> evaluated in the scope of the module that defines the function, which
>>> seems to be the Right Thing to do, but I could not find this specified
>>> in the manual. Did I miss it or is it just not spelled out? Asking
>>> because I would submit a correction if it is the intended behavior. Eg
>>
>> This is just the default lexical scope. Note that it is *not* the
>> module that defines the function for closures. There's already a
>> section about the scope of default arguments so it is fine if you want
>> to submit something short that clarify this a little bit.
>
> I wanted to submit a clarification but I don't understand your sentence
>
> "it is *not* the module that defines the function for closures."
>
> In the example below, I would reason about scope like this: (a
> different) bar is in the global scope for both modules, but the local
While true, the `bar` outside the scope `foo` is defined in is
irrelevant (in the sense that if `bar` isn't defined in `Foo`, `foo`
won't take it from anywhere else).
> scope of Foo.foo just takes it from Foo where it was defined (because of
> lexical scope). Is this reasoning correct?
What I mean by "not the module" is that
```
julia> function g()
a = 1
(b=a) -> b
end
g (generic function with 1 method)
julia> a = 2
2
julia> g()()
1
```
This is mention in the doc
http://julia.readthedocs.io/en/latest/manual/functions/#evaluation-scope-of-default-values
> the b in a=b refers to a b in an outer scope
after the example.
>
>>
>>>
>>> --8<---------------cut here---------------start------------->8---
>>> module Foo
>>> bar=1
>>> foo(a=bar)=a
>>> end
>>>
>>> module Baz
>>> import Foo.foo
>>> bar=2
>>> foo2()=foo()
>>> end
>>>
>>> Baz.foo2() # will then eval to 1
>>> --8<---------------cut here---------------end--------------->8---
>>>
>>> Best,
>>>
>>> Tamas