heh. that is ugly but serviceable. i understand what you mean about scoped / global. but it still seems like retrospective justification for what i assume is efficiency.
anyway, i can live with your workaround, thanks. andrew On Wednesday, 27 May 2015 10:24:26 UTC-3, Yichao Yu wrote: > > On Wed, May 27, 2015 at 9:17 AM, andrew cooke <[email protected] > <javascript:>> wrote: > > > > I don't want to define a global method. I want this to be local to the > > scope where it's defined. > > ``` > julia> foo(::Integer) = print("integer") > foo (generic function with 1 method) > > julia> foo(42) > integer > julia> function bar(i) > foo(i) > end > bar (generic function with 1 method) > > julia> bar(42) > integer > julia> function baz(i) > foo(s::ASCIIString) = print("string") > foo(args...) = (global foo; foo(args...)) > foo(i) > end > baz (generic function with 1 method) > > julia> baz(42) > integer > julia> baz("42") > string > ``` > > (note that the inner function doesn't has to be called "foo", and in > that case the `global foo` is not necessary) > > > > > > Is that not possible? It seems kind of odd that the main dispatch tool > in > > the language isn't scoped. :o( > > Generic functions are scoped, as you have shown with your first > example. What's not scoped is extending/assigning to them (just like > any assignment/mutation). You should probably think of defining a > method as pushing to an array (and it pretty much is). The array is > scoped, but if you push to a global one, the effect will be global. > > > > > Andrew > > > > On Wednesday, 27 May 2015 08:44:57 UTC-3, Mauro wrote: > >> > >> you have to declare foo as global otherwise it makes a new generic > >> function: > >> > >> julia> function baz(i) > >> global foo(s::ASCIIString) = print("string") > >> foo(i) > >> end > >> baz (generic function with 1 method) > >> > >> julia> baz(4) > >> integer > >> > >> > >> On Wed, 2015-05-27 at 13:33, andrew cooke <[email protected]> wrote: > >> > I have a bad feeling I have asked this before, but can't find the > >> > thread, > >> > sorry. How do I make the following work as expected (ie print > >> > "integer") > >> > instead of giving an error? > >> > > >> > julia> foo(i::Integer) = print("integer") > >> > foo (generic function with 1 method) > >> > > >> > julia> foo(42) > >> > integer > >> > julia> function bar(i) > >> > foo(i) > >> > end > >> > bar (generic function with 1 method) > >> > > >> > julia> bar(42) > >> > integer > >> > julia> function baz(i) > >> > foo(s::ASCIIString) = print("string") > >> > foo(i) > >> > end > >> > baz (generic function with 1 method) > >> > > >> > julia> baz(42) > >> > ERROR: `foo` has no method matching foo(::Int64) > >> > in baz at none:3 > >> > > >> > Thanks, > >> > Andrew > >> > > >
