Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement method dispatch for a union of functions #54620

Open
nsajko opened this issue May 29, 2024 · 1 comment
Open

implement method dispatch for a union of functions #54620

nsajko opened this issue May 29, 2024 · 1 comment
Labels
feature Indicates new feature / enhancement requests types and dispatch Types, subtyping and method dispatch

Comments

@nsajko
Copy link
Contributor

nsajko commented May 29, 2024

I'd like this to work:

function f end
function g end
function (::Union{typeof(f),typeof(g)})()
    7
end

using Test
@test f() === g()

However, it doesn't work:

julia> function f end
f (generic function with 0 methods)

julia> function g end
g (generic function with 0 methods)

julia> function (::Union{typeof(f),typeof(g)})()
           7
       end
ERROR: Method dispatch is unimplemented currently for this method signature
Stacktrace:
 [1] top-level scope
   @ REPL[3]:1

julia> versioninfo()
Julia Version 1.12.0-DEV.617
Commit 337b952625d (2024-05-29 13:31 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × AMD Ryzen 3 5300U with Radeon Graphics
  WORD_SIZE: 64
  LLVM: libLLVM-17.0.6 (ORCJIT, znver2)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)

This would occasionally be nice for defining a single method for several functions, to prevent code duplication.

@nsajko nsajko added types and dispatch Types, subtyping and method dispatch feature Indicates new feature / enhancement requests labels May 29, 2024
@nsajko
Copy link
Contributor Author

nsajko commented Jun 26, 2024

NB: the same thing, method for a Union, works fine as long as none of the types in the Union were defined with function. Example:

julia> struct T <: Function end

julia> struct S <: Function end

julia> (::Union{S,T})() = 7

julia> S()()
7

julia> @which S()()
(::Union{S, T})()
     @ Main REPL[3]:1

julia> versioninfo()
Julia Version 1.12.0-DEV.766
Commit 9d8ecaa899d (2024-06-21 17:00 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × AMD Ryzen 3 5300U with Radeon Graphics
  WORD_SIZE: 64
  LLVM: libLLVM-17.0.6 (ORCJIT, znver2)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Indicates new feature / enhancement requests types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

No branches or pull requests

1 participant