Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a warning when I overwrite a function in Julia?

Tags:

julia

Julia by default imports many names into scope. Is there a way to get a warning when I inadvertently overwrite one of them?

like image 364
becko Avatar asked Apr 04 '16 12:04

becko


1 Answers

In the context of modules and Base functions, Julia already does warn you if you overwrite a name. See the below examples that work on v0.4.5:

MODULES:

In modA.jl:

module modA

export test

function test()
    println("modA")
end
end

In modB.jl:

module modB

export test

function test()
    println("modB")
end
end

In REPL:

julia> using modA
julia> using modB
WARNING: Using modB.test in module Main conflicts with an existing identifier
julia> test()
"modA"

BASE FUNCTIONS:

In REPL:

julia> function +(x::Float64, y::Float64)
    println("my addition")
end

julia> WARNING: module Main should explicitly import + from Base
WARNING: Method definition +(Float64, Float64) in module Base at float.jl:208 
overwritten in module Main at none:2.

As far as I am aware, this does not work with user defined functions; see below:

julia> function test(x::Float64, y::Float64)
    println("First Definition")
end

julia> test(1.0, 2.0)
First Definition

julia> function test(x::Float64, y::Float64)
    println("Second Definition")
end

julia> test(1.0, 2.0)
Second Definition

Did you have a different context in mind for imported names?

like image 194
hyperdelia Avatar answered Nov 13 '22 18:11

hyperdelia