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

Methods may be added to a function from a different module without it being explicitly imported #54753

Closed
jishnub opened this issue Jun 10, 2024 · 2 comments

Comments

@jishnub
Copy link
Member

jishnub commented Jun 10, 2024

julia> module A1
       using LinearAlgebra
       struct A end
       Diagonal(rand(2))
       Diagonal(::A) = Diagonal(rand(2))
       end
Main.A1

julia> methods(A1.Diagonal)
# 8 methods for type constructor:
 [1] LinearAlgebra.Diagonal(::Main.A1.A)
     @ Main.A1 REPL[1]:5
[other methods...]

How am I able to add a method to LinearAlgebra.Diagonal without explicitly importing it, or qualifying it by the module name? The docs imply that this is required.

This doesn't work for Base functions, where adding the method fails with an error message stating that one needs to import the function explicitly.

julia> module A1
       struct A end
       sin(2)
       sin(::A) = sin(2)
       end
ERROR: invalid method definition in A1: function Base.sin must be explicitly imported to be extended
Stacktrace:
 [1] top-level scope
   @ none:0
 [2] top-level scope
   @ REPL[1]:4

I think this behavior should extend to other modules as well, and one shouldn't be able to add the method above.

julia> versioninfo()
Julia Version 1.10.4
Commit 48d4fd48430 (2024-06-04 10:41 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, tigerlake)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)
Environment:
  LD_LIBRARY_PATH = :/usr/lib/x86_64-linux-gnu/gtk-3.0/modules
  JULIA_EDITOR = subl
@KristofferC
Copy link
Member

#25744 I think

@jishnub
Copy link
Member Author

jishnub commented Jun 10, 2024

Yes, seems to be a duplicate

@jishnub jishnub closed this as completed Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants