I have a problem like this:
main.f90 --> contains MAIN file
sub_A.f90 --> contains subroutine A
sub_B.f90 --> contains subroutine B
other_stuffs.f90 --> contains all functions required by points 2 and 3.
All four points are written separately.
main.f90
include sub_A.f90
include sub_B.f90
include other_stuffs.f90
program MAIN
use A
use B
use other
...
call proc_A
call_proc_B
end program MAIN
sub_A.f90
module A
contains
subroutine proc_A
use other
...
call compute_something_1
end subroutine proc_A
end module A
sub_B.f90
module B
contains
subroutine proc_B
use other
...
call compute_something_2
end subroutine proc_B
end module B
other_stuffs.f90
module other
contains
subroutine compute_something_1
...
end subroutine compute_something_1
subroutine compute_something_2
...
end subroutine compute_something_2
end module other
Unfortunately, it didn't work. Did I do something wrong?
You are missing a contains section in the module other.
Only subroutines, functions and variables that are located in the contains section are accessible from outside the module.
module other
contains
subroutine compute_something_1
...
end subroutine compute_something_1
subroutine compute_something_2
...
end subroutine compute_something_2
end module other
The answer should be as follow
main.f90
include other_stuffs.f90 !It becomes the first now
include sub_A.f90
include sub_B.f90
program MAIN
use other
use A
use B
...
call proc_A
call_proc_B
end program MAIN
sub_A.f90
module A
use other ! CORRECT PLACE
contains
subroutine proc_A
! use other !WRONG PLACE !!!
...
call compute_something_1
end subroutine proc_A
end module A
sub_B.f90
module B
use other ! CORRECT PLACE
contains
subroutine proc_B
! use other !WRONG PLACE !!!
...
call compute_something_2
end subroutine proc_B
end module B
other_stuffs.f90
module other
contains
subroutine compute_something_1
...
end subroutine compute_something_1
subroutine compute_something_2
...
end subroutine compute_something_2
end module other
Thanks for all.
Related
I am kind of new to fortran and have come across the following problem. Let us consider the following simple "output.for":
implicit none
character*10 str1,str2
call get_command_argument(1,setupfile)
open(20, file = setupfile)
read(20,*) str1, str2
write(6,*) str1 ! I want this to go to terminal output
write(6,*) str2 ! I'd like to have this redirected to a file
end
And now I invoke the code as: ./output setupfile > outfile.dat
How do I force "str1" to be printed to terminal output while "str2" to be redirected to outfile.dat?
In OCaml, if your project has a file called code.ml you can refer to it in other files using the module name Code. I was wondering if you defined an .mli file if you could refer to the signature it defines in a similar way. For example if you had a file called wow.mli and you could have another file with the declaration
module Func(St : Wow) = struct ... end
Is there a way to do something along those lines?
This works for me:
module Func(St: module type of Wow) = struct ... end
In detail here's what I did:
$ cat wow.mli
val f : int -> int
$ cat m.ml
module Func (St: module type of Wow) = struct let f x = St.f x end
$ ocamlopt -c wow.mli
$ ocamlopt -c m.ml
I have this module in my lib folder:
module Exceptions
class NotAuthorized < StandardError
end
end
Then, I have a test for a controller like this:
class Admin::ApplicationControllerTest < ActionController::TestCase
test "should not be authorized" do
assert_raise(Exceptions::NotAuthorized) {
get :index
}
end
end
I also try to put this line on my environments/test.rb
config.autoload_paths +=%W(#{config.root}/lib)
But, when I run the test I always get:
NameError: unitialized constant Admin::ApplicationControllerTest::Exceptions
How to include the module Exceptions in the test?
Moving
config.autoload_paths +=%W(#{config.root}/lib)
to application.rb fix it.
Suppose I have the following directory structure for the project:
myproj/dir1/file1.d
myproj/dir2/file2.d
myproj/main.d
How can I import main and file2 modules within the source file file1.d?
file1.d will have module dir1.file1; line , file2.d will have module dir2.file2; line and main.d will start with module main;.
Module declarations above will tell D what to do when it encounters line like: import main, dir2.file2;;
As suggested by #sigod , read the http://dlang.org/module.html for more information.
How can I emulate this Python idiom in OCaml?
if __name__=="__main__":
main()
See RosettaCode for examples in other programming languages.
There is no notion of main module in Ocaml. All the modules in a program are equal. So you can't directly translate this Python idiom.
The usual way in Ocaml is to have a separate file containing the call to main, as well as other stuff like command line parsing that only make sense in a standalone executable. Don't include that source file when linking your code as a library.
There is a way to get at the name of the module, but it's rather hackish, as it is intended for debugging only. It violates the usual assumption that you can rename a module without changing its behavior. If you rely on it, other programmers reading your code will curse you. This method is provided for entertainment purposes only and should not be used in real life.
let name_of_this_compilation_unit =
try assert false with Assert_failure (filename, _, _) -> filename
You can compare the name of the compilation unit with Sys.executable_name or Sys.argv.(0). Note that this is not really the same thing as the Python idiom, which does not rely on the toplevel script having a particular name.
$ ocamlc -o scriptedmain -linkall str.cma scriptedmain.ml
$ ./scriptedmain
Main: The meaning of life is 42
$ ocamlc -o test -linkall str.cma scriptedmain.ml test.ml
$ ./test
Test: The meaning of life is 42
scriptedmain.ml:
let meaning_of_life : int = 42
let main () = print_endline ("Main: The meaning of life is " ^ string_of_int meaning_of_life)
let _ =
let program = Sys.argv.(0)
and re = Str.regexp "scriptedmain" in
try let _ = Str.search_forward re program 0 in
main ()
with Not_found -> ()
test.ml:
let main () = print_endline ("Test: The meaning of life is " ^ string_of_int Scriptedmain.meaning_of_life)
let _ =
let program = Sys.argv.(0)
and re = Str.regexp "test" in
try let _ = Str.search_forward re program 0 in
main ()
with Not_found -> ()
Posted on RosettaCode.