#NAME with module and function of the same name - vba

I created a module called foo in my spreadsheet, and then I added the following to it:
Function foo() As Variant
foo = 5
End Function
When I try to run the function in Excel, by typing =foo() into a cell, I get #NAME. When I look at what #NAME is supposed to mean by clicking on the little icon next to it and then help on this error I get this:
Well, not exactly that, but it was about as useful.
Eventually I discovered that changing the module name to something other than foo seemed to fix it. Have I stumbled upon a bug or a feature? Where is this behavior documented?

Since multiple modules are possible and all can have public functions it is also possible that there are multiple public functions with the same name but in different modules. That's why you can call a UDF with =foo.foo(). This is calling the function named "foo" in the module named "foo". That's why =foo() will fail if there is a module named "foo" because foo is first evaluated as the module name.

Related

Get list of variables in the current namespace in Julia

I have a question that is similar to, but different than this one. I have a function within a module, and I would like to see what variables are defined inside the function namespace. In the other post, they said to use varinfo, but that seems to only work in the Main namespace. For example if I run this
module Test
function hello()
a = 1
varinfo()
return a
end
end
import .Test
Test.hello()
I get this error
WARNING: replacing module Test.
ERROR: UndefVarError: varinfo not defined
Is there a way to get a list of variables within a given namespace? What I am looking for is a function that when called, outputs all the available variables (a in my example) as well as available modules within the namespace.
PS. I would like to add, that varinfo is incredibly limiting because its output is a Markdown.MD, which cannot be iterated over. I would prefer a function that outputs variables and values in some sort of list or dictionary if possible.
varinfo is showing only the global variables.
If you want the local variables, you need to use the Base.#locals macro:
module Test
function hello()
a = 1
println(Base.#locals)
return a
end
end
And now you can do:
julia> Test.hello()
Dict{Symbol, Any}(:a => 1)
1
Is this what you want?
module Test
function hello()
a = 1
println(Main.varinfo(#__MODULE__; all=true, imported=true))
return a
end
end

Redundant export usage?

Is there any reason to use "export" in a file (which contains no modules) that will be included in another file later on? I came across this type of export usage when looking at some packages on GitHub, which made me wonder. For instance, consider Foo.jl:
# Foo.jl
export foo1
function foo1()
do something
end
function foo2()
do something
end
Which is included in Bar.jl
# Bar.jl
module Bar
include("Foo.jl")
other stuff
end
Won't the function foo2() be in the scope of Bar regardless, thus making the use of "export" totally unnecessary? I saw this type of stuff in several different packages, and don't really get the reason.
Thanks a lot in advance for any help,
Renato
These exports are not redundant. These exports are not about the scope of Bar but rather the scopes of other modules that import Bar. If you import the module Bar via using Bar in another module or in Main, the name foo1 will be public so that you only need to write just foo1 without qualifiers to access the function foo1 instead of Bar.foo1.
If you remove that export statement from Foo.jl you will see that you can no longer access the function foo1 without module name qualification after issuing using Bar. You either have to write Bar.foo1 or explicitly make foo1 visible in that module via, for example, using Bar: foo1 or import Bar: foo1.
The include statement simply makes Julia evaluate the code in that module so you can think of Bar.jl as if it is
# Bar.jl
module Bar
export foo1
function foo1()
do something
end
function foo2()
do something
end
other stuff
end
So the export statement exports the name of foo1 in Bar to the other modules that import the module Bar.
You can find more information about importing, exporting and module system in the Julia documentation.
Within a module, you can control which names from other modules are
visible (via importing), and specify which of your names are intended
to be public (via exporting).
https://docs.julialang.org/en/v1/manual/modules/index.html#modules-1

Using modules to load a group of related functions

I want to use Raku Modules to group some functions, I often use. Because these functions are all loosely coupled, I don't like to add them in a class.
I like the idea of use, where you can select, which functions should be imported, but I don't like it, that the functions, which are imported are then stored in the global namespace.
For example if I have a file my_util.pm6:
#content of my_util.pm6
unit module my_util;
our sub greet($who) is export(:greet) {
say $who;
}
sub greet2($who) is export(:greet2) {
say $who;
}
sub greet3($who) is export(:greet3) {
say $who;
}
and a file test.p6:
#!/usr/bin/perl6
#content of test.p6
use v6.c;
use lib '.';
use my_util :greet2;
greet("Bob"); #should not work (because no namespace given) and also doesn't work
greet2("Bob"); #should not work (because no namespace given) but actually works
greet3("Bob"); #should not work (because no namespace given) and also doesn't work
my_util::greet("Alice"); #works, but should not work (because it is not imported)
my_util::greet2("Alice"); #should work, but doesn't work
my_util::greet3("Alice"); #should not work (because it is not imported) and also doesn't work
I would like to call all functions via my_util::greet() and not via greet() only.
The function greet() defined in my_util.pm6 comes very close to my requirements, but because it is defined as our, it is always imported. What I like is the possibility, to select which functions should be imported and it should be possible to leave it in the namespace defined by the module (i.e. it doesn't pollute the global namespace)
Does anyone know, how I can achieve this?
To clear up some potential confusion...
Lexical scopes and package symbol tables are different things.
my adds a symbol to the current lexical scope.
our adds a symbol to the current lexical scope, and to the public symbol table of the current package.
use copies the requested symbols into the current lexical scope.
That's called "importing".
The :: separator does a package lookup – i.e. foo::greet looks up the symbol greet in the public symbol table of package foo.
This doesn't involve any "importing".
As for what you want to achieve...
The public symbol table of a package is the same no matter where it is referenced from... There is no mechanism for making individual symbols in it visible from different scopes.
You could make the colons part of the actual names of the subroutines...
sub foo::greet($who) is export(:greet) { say "Hello, $who!" }
# This subroutine is now literally called "foo::greet".
...but then you can't call it in the normal way anymore (because the parser would interpret that as rule 4 above), so you would have to use the clunky "indirect lexical lookup" syntax, which is obviously not what you want:
foo::greet "Sam"; # Could not find symbol '&greet'
::<&foo::greet>( "Sam" ); # Hello, Sam!
So, your best bet would be to either...
Declare the subroutines with our, and live with the fact that all of them can be accessed from all scopes that use the module.
Or:
Add the common prefix directly to the subroutine names, but using an unproblematic separator (such as the dash), and then import them normally:
unit module foo;
sub foo-greet($who) is export(:greet) { ... }
sub foo-greet2($who) is export(:greet2) { ... }
sub foo-greet3($who) is export(:greet3) { ... }

How to use the variable declared in one module to be used in other module function access vba

Is there any way that we can use variable declared in one module and use that variable in a function defined in other module?
Yes. Declare a variable at module level like this:
Public myvar As String
It can then be accessed from other modules.
Note, however, that global variables like this are almost never a good idea.

How to get the object of the module dynamically created in OMNet++ 4?

I'm a newbie in OMNet. In my project, I dynamically create a simple module, and I want to use the object created by this module. Does anyone can give me some help?
Source is here:
cModuleType* moduleType = cModuleType::get("Person");
cModule *mod = moduleType->create("per", this->getParentModule());
mod->buildInside();
mod->scheduleStart(simTime());
mod->callInitialize();
job->mod = mod;
Basically, I want to find the object related to the "mod".
Thank you
I'm not sure what you mean "find" the object you created. You already have the object you created, you probably just need to cast it to do anything useful with it.
If you mean you want to operate on the module "mod", you can do this by casting "mod" to the module type which you've declared (say MyModule).
MyModule *my_mod = check_and_cast<MyModule *>(mod);
You can then define some public functions in the class of MyModule (usually MyModule.cc) that do whatever you want to do.
MyModule::my_method() {some code}
If you've done this, in the current function, you can just go:
my_mod->my_method();
I hope this answers your question.