RLLL
RLLL

Reputation: 19

Modifying a function from a package in Julia

First I'm sorry if this question seems vague, because I'm new to Julia and I'm mostly just wondering in Julia if there's an easy way to modify a native function of a package while simply using all its dependencies in general, or there has to be a package-specific solution.

For example, foo is a function from the package LibraryA, and it can be simply called and used as follows without problem:

using LibraryA 

foo(args)

However, if I want a slightly modified version of foo(), say bar(), whose source code (mostly copy of the foo() function) is in a separate file bar.jl and uses a lot of dependencies from LibraryA, the lines

using LibraryA 

include("bar.jl")
bar(args)

will not work as lots of loaderrors occur:

LoadError: UndefVarError: vars not defined

where vars are the variables defined in the package LibraryA

Upvotes: 1

Views: 441

Answers (1)

Sundar R
Sundar R

Reputation: 14695

As long as the variables are in the global scope in LibraryA, you can import them explicitly to make them available in the current scope:

using LibraryA: vars, bigvar, littlevar, baz

include("bar.jl")
bar(args)

As a matter of convention, however, make sure that these variables are not intended to be "private" i.e. intended for internal use in LibraryA (there's no explicit private marker in Julia, it's generally inferred based on documentation).

If a variable vars itself appears to be private, but has an accessor getvars that's public, you can instead create a new variable vars in the current scope using that accessor.

using LibraryA: bigvar, littlevar, baz, getvars

vars = getvars()

include("bar.jl")
bar(args)

(Make sure to make it a const var if it was const originally, and similarly type annotate it var::WhateverTheTypeOfVarIs if it is annotated in the original module as well.)

Upvotes: 5

Related Questions