Hi folks,
I've been working on implementing the "merging" functionality that Jeff
Bezanson noted earlier in this thread. For those interested, the project
can be found here: https://github.com/davidagold/MetaMerge.jl. People
should feel free to do whatever they find useful (if anything) with the
code. Here's an example of it in action in Julia 3.7:
julia> using MetaMerge
julia> f()=nothing
f (generic function with 1 method)
julia> module A
export f
immutable Foo end
f(::Foo) = print("This is Foo.")
f(x::Int64) = x
end
julia> module B
export f
immutable Bar end
f(::Bar) = print("This is Bar.")
f(x::Int64) = 2x
end
julia> using A, B
Warning: using A.f in module Main conflicts with an existing identifier.
Warning: using B.f in module Main conflicts with an existing identifier.
julia> methods(f)
# 1 method for generic function "f":
f() at none:1
julia> metamerge(f, A, B)
f (generic function with 3 methods)
julia> methods(f)
# 3 methods for generic function "f":
f() at none:1
f(x1::Foo)
f(x1::Bar)
julia> f(A.Foo())
This is Foo.
julia> f(B.Bar())
This is Bar.
julia> metamerge(f, A, B, conflicts_favor=A)
f (generic function with 4 methods)
julia> methods(f)
# 4 methods for generic function "f":
f() at none:1
f(x1::Foo)
f(x1::Bar)
f(x1::Int64)
julia> f(2)
2
It's currently designed mostly for use in the REPL; I expect that making it
more suitable for general coding will require taking advantage of some
updates coming in Julia 4.0.
Cheers,
David
On Tuesday, April 21, 2015 at 9:26:01 AM UTC-4, Michael Turok wrote:
>
> Hi,
>
> What is the idiomatic way to create a function value() in different
> modules, dispatched on different arguments, without getting the
> warning/error about conflicting with an existing identifier?
>
> It seems like there is an order dependency with the example below. Seems
> like the 2nd module defines value(), unless you had already used value()
> prior to importing the 2nd module.
>
> Note that if I do the same with get() a function defined in Base, I don't
> get an error.
>
> Code and output from julia REPL below.
>
> Any help appreciated,
> Michael
>
> # this is mike.jl
>
> # ------------------------------
> module Foo
> # ------------------------------
> importall Base
> type FooType end
>
> value(x::FooType) = "Foo::value"
> get(x::FooType) = "Foo::get"
>
> export value
>
> end
>
> # ------------------------------
> module Bar
> # ------------------------------
> importall Base
>
> type BarType end
>
> value(x::BarType) = "Bar::value"
> get(x::BarType) = "Bar::get"
>
> export value
>
> end
>
> Using this in the REPL:
> julia> workspace() ; include("mike.jl")
>
> julia> using Foo
>
> julia> value(Foo.FooType())
> "Foo::value"
>
> julia> using Bar
> Warning: using Bar.value in module Main conflicts with an existing
> identifier.
>
> julia> value(Bar.BarType())
> ERROR: `value` has no method matching value(::BarType)
>
> # -----------------------------------------------------
>
> julia> workspace() ; include("mike.jl")
>
> julia> using Foo
>
> julia> using Bar
>
> julia> value(Foo.FooType())
> ERROR: `value` has no method matching value(::FooType)
>
> julia> value(Bar.BarType())
> "Bar::value"
>
> # -----------------------------------------------------
>
> julia> workspace() ; include("mike.jl")
>
> julia> using Bar
>
> julia> using Foo
>
> julia> value(Foo.FooType())
> "Foo::value"
>
> julia> value(Bar.BarType())
> ERROR: `value` has no method matching value(::BarType)
>
> julia>
>