Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Is there a way to define a 'module' variable?

$
0
0

I’m struggling a bit with scopes.

From the documentation, this works:

module Bar
       x = 1
       foo() = x
       export foo
end

using .Bar
foo()

But this does not:

module Tofu
       counter = 0
       f() = (counter += 1; counter)
       export f
end

using .Tofu
f()

The latter gives me a ‘counter not defined’ error.

I’m guessing this is an implementation thing. My guess is that the compiler binds 1 to f() in the first example so x is actually never referenced in the call to f(). But from a coding perspective this makes no sense since the first and second example should produce similar results from that perspective.

Is there a way to define a variable which is accessible within the module but not outside it except when it is explicitly referenced?

Thanks in advance,
Stef

4 posts - 4 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Latest Images

Trending Articles



Latest Images