Function signature not found despite showing with methods(…)

前端 未结 1 1717
滥情空心
滥情空心 2021-01-05 13:37

I am new to Julia, so this might be trivial.

I have a function definition within a module that looks like (using URIParser):

function add!(graph::Gra         


        
相关标签:
1条回答
  • 2021-01-05 14:14

    This looks like you may be getting bit by the very slight difference between method extension and function shadowing.

    Here's the short of it. When you write function add!(::Graph, ...); …; end;, Julia looks at just your local scope and sees if add! is defined. If it is, then it will extend that function with this new method signature. But if it's not already defined locally, then Julia creates a new local variable add! for that function.

    As JMW's comment suggests, I bet that you have two independent add! functions. Base.add! and RDF.add!. In your RDF module, you're shadowing the definition of Base.add!. This is similar to how you can name a local variable pi = 3 without affecting the real Base.pi in other scopes. But in this case, you want to merge your methods with the Base.add! function and let multiple dispatch take care of the resolution.

    There are two ways to get the method extension behavior:

    1. Within your module RDF scope, say import Base: add!. This explicitly brings Base.add! into your local scope as add!, allowing method extension.

    2. Explicitly define your methods as function Base.add!(graph::Graph, …). I like this form as it more explicitly documents your intentions to extend the Base function at the definition site.

    This could definitely be better documented. There's a short reference to this in the Modules section, and there's currently a pull request that should be merged soon that will help.

    0 讨论(0)
提交回复
热议问题