I'm trying to use a function that is inside a file (module 1) in another file (module) 2. But it gives me an error "ERROR: UndefVarError: t1 not defined". I have tried to export the function in module 1 but it also doesn't work. I'm new to Julia and I do not know very well how to handle modules.
Here is the code that I'm having problems.
First File: t1.jl
module A
function soma(a::Int64, b::Int64)
return a + b
end
end
Second File: t2.jl
module B
include("t1.jl")
using .t1
function main()
s = soma(2, 3)
println(s)
end
main()
end
Changing t2.jl to:
module B
include("t1.jl")
using .A
function main()
s = A.soma(2, 3)
println(s)
end
main()
end
prints out 5 as expected.
include is basically as if you'd copy-pasted the code from the included file into the current file - so, once you've included t1.jl into module B, the fact that the file is called t1.jl has no relevance. It's module A that's in the current scope, and A is the namespace that contains the soma function we need. Hence, using .A is what's needed to make soma available within B.
Related
I'm starting to code with vb.net and i need to run the code directly, like it happens with java: in the cmd i can run the class files. Is there any similar possibility with vb.net, preferably directly from the visual studio hub?
Thanks!
You can use the Immediate Window for that. It offers many different ways to interact with your code. To use it, start your application in debug mode from Visual Studio and press CTRL + ALT + I.
To execute a shared method you can type in the Immediate Window:
className.methodName()
(example)
MainFunctions.DoStuff()
DoMoreStuff()
Where className is optional if you're currently already inside the class (for example if you've hit a breakpoint in it).
If you want to execute an instance (non-shared) method you can either use the method above (without className, but you must currently be inside the class by hitting a breakpoint, for example), or you create a new instance of the class and execute the method:
Public Class MiscFunctions
Public Sub PrintHelloWorld()
Debug.WriteLine("Hello World!")
End Sub
End Class
(Immediate Window)
New MiscFunctions().PrintHelloWorld()
Hello World!
Dim m As New MiscFunctions
m.PrintHelloWorld()
Hello World!
You can also print the value of a variable or the return value of a function by typing:
? variableOrFunctionNameHere
(example)
? ImageCount
4
The same rules for executing methods applies to evaluating functions too:
Public Class MiscFunctions
Public Shared Function IsEven(ByVal Num As Integer) As Boolean
Return Num Mod 2 = 0
End Function
Public Function Sum(ByVal a As Integer, ByVal b As Integer) As Integer 'Non-shared method.
Return a + b
End Function
End Class
(Immediate Window)
? MiscFunctions.IsEven(3)
False
? MiscFunctions.IsEven(8)
True
? New MiscFunctions().Sum(3, 9)
12
You can also dynamically evaluate expressions:
? ImageCount + 1 = 5 'Here, ImageCount is 4
True
? 2 * 4
8
if you are looking for more then the Immediate Window. Look into Microsoft CodeDom. I have used it to compile C#, VB.net, C++, and Visual J#.
https://msdn.microsoft.com/en-us/library/y2k85ax6(v=vs.110).aspx
A plugin defines a function named HLMarks():
hi Marks term=reverse ctermfg=0 ctermbg=40 guibg=Grey40
function! HLMarks(group)
call clearmatches()
let index = char2nr('a')
while index < char2nr('z')
call matchadd( a:group, '\%'.line( "'".nr2char(index)).'l')
let index = index + 1
endwhile
endfunction
I want the HLMarks() function to run automatically every time vim opens a file.
It works when I call the function manually:
:call HLMarks("Marks")
Adding this line to the end of the plugin didn't do anything:
call HLMarks("Marks")
Calling the function from vimrc got this error:
E117: Unknown function: HLMarks
How to automatically call the HLMarks("Marks") function when a file is opened?
The plugin is described on http://www.vim.org/scripts/script.php?script_id=3394
and down loaded from http://www.vim.org/scripts/download_script.php?src_id=21611
The plugin's markHL.vim file is in my ~/.vim/plugin/ directory.
The ":function" command lists:
function HLMarks(group)
The solution is to add this line to vimrc:
autocmd BufReadPost * call HLMarks("Marks")
Details are at https://groups.google.com/forum/#!topic/vim_use/i2HWD_9V-28
If you define the function in .vimrc then:
function! yourFunc()
" ...
endfunction
call yourFunc()
simply adding the call yourFunc() after the definition will work.
I have a module that looks like this. I've simplified it greatly so as not to clutter up my question (my local functions are more complex than this). Here is my code:
decision = {}
function win(win_arg) return win_arg end
function lose(lose_arg) return lose_arg end
local function draw(draw_arg) return draw_arg end
function decision.get_decision(func, arg)
return func(arg)
end
return decision
I am calling my module with the code below.
my = require "my-lua-script"
print(my.get_decision(lose, "I lose."))
print(my.get_decision(win, "I win."))
I want 'get_decision' to be a public method. I want win, lose and draw to be private, but I want to call them dynamically from get_decision. If I understand correctly, win and lose are in the global namespace right now? If I put a local in front of these two methods (like draw) then my code doesn't work.
Is there a way to accomplish what I am after?
Of course.
my-script.lua
-- This is the local side of the module.
local outcomes = {}
function outcomes.win(arg) return arg end
function outcomes.lose(arg) return arg end
function outcomes.draw(arg) return arg end
-- This is the exposed side of the module.
local M = {}
function M.get_decision(outcome, arg)
return outcomes[outcome](arg)
end
return M
main.lua
local my = require'my-script'
print(my.get_decision('win', 'I win.'))
print(my.get_decision('lose', 'I lose.'))
You simply use a string to indicate which function you'd like to access, and use that string to index a table of functions from within get_decision against outcomes. This will keep the functions hidden behind get_decision.
I have a class file myClass.m in a package folder +myPack that's on the path. A simple example of the class file is:
classdef myClass
properties
prop
end
methods
function obj = myClass(x)
obj.prop = x;
end
end
end
Now if I directly call the method and access the property using the full package name, i.e.:
x = myPack.myClass(2).prop;
returns x = 2 correctly. Now, if I try the same by importing this class (and not use the package name):
import myPack.myClass
y = myClass(2).prop
it gives me the following error:
Static method or constructor invocations cannot be indexed.
Do not follow the call to the static method or constructor with
any additional indexing or dot references.
Why does this work in the first case and not the second? As far as I understood, importing a class mainly allowed one to use the class name without the long package name (among other considerations). What is the difference in these two that causes this error and how can I work around it?
Here is some more weird for you: the behavior is different if you are running in the command window, from a script, or from a function!
1) command prompt (1st: ok, 2nd: error)
This is what you've already shown
>> x = myPack.myClass(2).prop
x =
2
>> import myPack.myClass; y = myClass(2).prop
Static method or constructor invocations cannot be indexed.
Do not follow the call to the static method or constructor with
any additional indexing or dot references.
2) Script (1st: error, 2nd: error)
testMyClassScript.m
x = myPack.myClass(2).prop
import myPack.myClass; y = myClass(2).prop
and
>> testMyClassScript
Static method or constructor invocations cannot be indexed.
Do not follow the call to the static method or constructor with
any additional indexing or dot references.
Error in testMyClassScript (line 1)
x = myPack.myClass(2).prop
(the second line would also throw the same error)
3) Function (1st: ok, 2nd: ok)
testMyClassFunction.m
function testMyClassFunction()
x = myPack.myClass(2).prop
import myPack.myClass; y = myClass(2).prop
end
and
>> testMyClassFunction
x =
2
y =
2
I would definitely call that a bug :) The expected behavior is to give an error in all cases.
I'm fairly new with OCaml Module and I haven't managed to use my own module without combining both an "include" and an "open".
I've tried to put the signature in a separate .mli file, without success.
Below I'm indicated a minimum (not) working example, that I'm trying to compile with
ocamlc -o main Robot.ml main.ml
What to I need to do to only have to use "open", or only "include", but not both of them ?
File "Robot.ml" :
module type RobotSignature =
sig
val top: unit -> unit
end
module Robot =
struct
let top () =
begin
Printf.printf "top\n"
end
(* Should not be visible from the 'main' *)
let dummy () =
begin
Printf.printf "dummy\n"
end
end
File "main.ml" (not working) :
open Robot;;
top();
File "main.ml" (working) :
include Robot;;
open Robot;;
top();
You've got two levels of Robot. Since you explicitly called your module "Robot" within the file robot.ml you'll need to open Robot and then call Robot.top(). Anything in the robot.ml file is already put implicitly inside of a Robot module.
You could get rid of the extra 'module Robot' declaration in robot.ml.
robot.ml would become:
module type RobotSignature =
sig
val top: unit -> unit
end
let top () =
begin
Printf.printf "top\n"
end
Then it should work as you have it in your main.ml.
Update based on comment below: If you're concerned that everything in robot.ml will now be visible when you 'open Robot' you can define a robot.mli file which specifies the functions which are available externally. For example, let's say you add a function called helper in robot.ml:
let top () =
begin
Printf.printf "top\n"
end
let helper () =
Printf.printf "helper\n"
...and then you define your robot.mli as follows:
val top: unit -> unit
Then let's say you try to call helper from main.ml:
open Robot;;
top();
(* helper will not be visible here and you'll get a compile error*)
helper ()
Then when you try to compile you'll get an error:
$ ocamlc -o main robot.mli robot.ml main.ml
File "main.ml", line 4, characters 0-6:
Error: Unbound value helper
You have two ways to do this:
First, you can constrain your sub-structure to be of the right signature:
module Robot : RobotSignature = struct ... end
Then in main.ml, you can do open Robot.Robot: the first Robot means the compilation unit associated to robot.ml, the second Robot is the submodule you have defined inside robot.ml
You can also remove one level and create robot.mli containing:
val top: unit -> unit
and robot.ml containing:
let top () =
Printf.printf "top\n"
(* Should not be visible from the 'main' *)
let dummy () =
Printf.printf "dummy\n"
You can compile the modules using ocamlc -c robot.mli && ocamlc -c robot.ml and then in main.ml simply use open Robot.