Is there a way to open a module in Erlang and then call its functions without using module name prefix?
Like opening an ML structure!
You can use
-import(my_module, [foo/1,bar/2]).
to import individual functions (in my example foo/1 and bar/2) from another module (my_module), see the modules documentation . There is no way of importing all functions from a module, they have to be explicitly listed.
Also see In Erlang how can I import all functions from a module? for an explanation why you shouldn't import functions!
No, you can't! The methods given by #johlo and #stemm are just work-arounds which allow you to not explicitly write the module name but that is all. The -import(...) declaration is a misnomer and doesn't do what you would expect.
Given Erlang's very dynamic handling of code it would be practically meaningless as well. There is no guarantee that at run-time you have the same "other" module as you had at compile-time, or if it is there at all. All code handling, compiling/loading/purging/reloading/etc. , is done on a module basis and there are no inter-module dependencies or optimisations.
Instead of import you can use defining:
-define(SIN(X), math:sin(X)).
my_func(X) -> ?SIN(X).
Related
In my work, we have a file with utilities functions that are extended in _.mixin() function at runtime. No, I cannot change the framework, because the solution would be to separate those function from _.mixin() and make them and indepent module, that would be the best.
Either way, my problem is that I cannot link this file, with all the extended functions in WebStorm, so I always see Unresolved function or method warning
Yes I have tried to add the file in the Library tab under Libraries & Framework preferences. And it did nothing.
So my question is, It is possible to WebStorm (or other Atlassian software) to link a file with several functions that will be extended with Underscore.js _.mixin() function to show up in Autocomplete?
Thanks, in advance
Adding the file to JavaScript libraries won't help here. _.mixin() dynamically adds passed utility functions to _ object, it's not possible to resolve such dynamically generated stuff during static code analysis unless a special treatment for the certain functions is provided. And WebStorm doesn't provide any special support for _.mixin().
If you miss it, please feel free to create a feature request in youtrack, https://youtrack.jetbrains.com/issues/WEB
Is there a way to export all symbols in a Julia module (something like the semantic counterpart to importall)? Such a functionality would be very useful when the number of symbols to be exported grows large. TIA.
There's the Reexport.jl package that provides a form of this; it's use case is when you have a submodule and you want to reexport all of the exported symbols from the inner module into your current module. I know, not exportall, but part of the functionality.
I wrote a minimal ExportAll.jl package. It works and is tested for Julia 1.1.1 and it is available as a Julia package. Basically, it exports all symbols(!) defined in a module that is not part of any of the core modules. Disclaimer doing things like this is not good practice and should only be done with great care.
I come from Java backgrounds and the problem of packaging is as follows then:
I can have many files under the same package, say com.parobay.io. I can then distribute this as a library, and the users will use it like this:
import com.parobay.io.Input;
import com.parobay.io.Output;
or
import com.parobay.io.*; // to import everything
So I can have a single "module (com.parobay.io) and classes defined in multiple files.
So how to I achieve the same in D? Do I have to create a directory com\parobay\io and there place two files called Input.d and Output.d or is there a smarter way?
In Java the rules are very strict, so it's hard to get it wrong. In D there are many possibilities. So are there any conventions, like one class per file, or file name equal to the name of class?
You can choose to do it basically the same as Java, though remember these items:
import foo.* doesn't work in D, but you CAN make a file called package.d in the directory which manually lists public import foo.Input; public import foo.Output; etc. which allows you to import the whole package.
ALWAYS put a module com.parobay.io.Input; or whatever line at the top of any file that is imported. Don't expect it to just work based on directory structure and filename. The directory structure actually isn't strictly necessary, it is just a convention to easily find the file. The module line at the top with the name is the authoritative thing the compiler checks.
D modules often have all lowercase names, but you can use uppercase ones if you like. I think it is nice to use a lowercase name like the class name, so you might call the module io.input and the class Input. The reason for this convention is sometimes filename case gets lost when transferring from system to system. But developers are pretty aware of case so in practice either way should work.
One class per file will work fine or you can put two tightly coupled classes together in the same file (they'll have access to each other's private members if they are in the same file).
See this page for more info: http://dlang.org/module especially search for the heading "Package Module"
Don't use two separate files for your Input and Output classes. Instead, put both classes in a single file, parobay/io.d (corresponding to the module parobay.io).
It's definitely not the convention to limit yourself to just one class per file. D modules are for grouping together code of related functionality. When someone does import parobay.io;, they expect to get all of parobay.io - classes, utility functions and whatever else is relevant. It's similar to Java's import com.parobay.io.*;.
If someone really wants to import specific parts of your module, they can use selective imports:
import parobay.io: Input; // Just the Input class of the parobay.io module.
import parobay.io: Output; // Just the Output class.
import parobay.io: Input, Output; // Just the Input and Output classes.
There are a few additional things to note about this.
Package and module names are conventionally all-lowercase.
It makes everyone's lives easier if the path to the file matches up exactly with its full module name. For example, module foo.bar.baz should be in the file foo/bar/baz.d.
In my experience, it's rare for a D module to be named after a domain name. You can prefix your module names with com or org or net if you really want to, but it's not expected like it is in Java.
Adam D. Ruppe's answer has some great points about explicit module declarations and class member visibility. It's also well worth reading the module and style pages on the official D website.
D community has three widely accepted alternatives.
Write a module named all.d which includes all modules from your package. (Literally '*' --> 'all'). After that you simply do import com.paroboy.io.all;
I see more and more that D developers use _ for this. So they write a module called _.d for this purpose. Similarly to #1, you do import com.paroboy.io._;
Relatively new addition to the D programming language is the package.d module, which can be used to import the package. More about this at the following DIP: http://wiki.dlang.org/DIP37 . If I remember well, DMD supports it since v2.064 . (Documentation: http://dlang.org/module#PackageModule)
I myself use the #1 approach because it is obvious what is going on. While #2 and #3 may be confusing to people reading D source file, especially the third one. A valid question someone may ask: "What the heck am I importing, package?? But import is only for modules!??"
Allthough nothing stops you from having separate module per-class, I would not recommend it. D is truly modular language, so take advantage of that. Group all your types in a single D module. That is my advice, and that is the "D way".
NOTE:
There is a (big) semantic difference between Java "module" and a D module, as you have probably already noticed. I am primarily a Java programmer, so I know how confusing this may be to Java programmers who are playing with D. Java classes in the same package quite often take advantage of the package-level access. However, classes within the same module behave like "friends" in C++.
Speaking about Java modules, they were supposed to come with Java 8 (true modules!), but were posponed and will hopefully be included in Java 9.
UPDATE: We reached conclusion, after a chat on FreeNode (IRC) with some members of the D-Programming-Language, that it is indeed safe now to use the package attribute. It behaves as the specification says.
I try to use two external Fortran module which are in same name (in this case mod_param). So, when i try to compile my code, the compiler gives the following error,
mod_param.o: In function mod_param._':
mod_param.f90:(.text+0x0): multiple definition ofmod_param._'
mod_param.o:mod_param.F90:(.text+0x0): first defined here
is there any way to solve it without renaming the one of the module file and its name? I don't prefer the renaming because the external modules are maintained by someone else and i don't want to play with them. Is there any special use statement to do that?
No. It is necessary to change the function name in the source code in at least one of the modules.
Since the code is being maintained by someone else, consider automating the renaming: perhaps the project Makefile can run a sed script which changes the function names. So that the dependencies are clear, be sure to make the output of the sed script a new file name which is used to be compiled—the virgin module would have a filename which is not compiled or linked into the project.
Even if it were somehow possible to link them both in with the same name, how would you control which was called with the name?
According to F2003 standard module names are global entities and must be unique in a program, with some exteptions for intrinsic modules.
So, that would be a no (Besides, how would you tell them apart were they of the same name?)
I'm an Erlang beginer and I'd like to find a way to list all the methods available for a given module. What's the best way?
In my case, the module is ejabberd_odbc.
You can call Modulename:module_info() to get information on the module in a proplist form. To get exports only, call Modulename:module_info(exports).
I'm always checking the exports section in the source of the module.
There is a tool you can install called edoc, whose manual is at: http://www.erlang.org/doc/man/edoc.html This allows javadoc style documentation to be generated for Erlang modules.
Also there is ejabberd_odbc documentation at: http://www.process-one.net/docs/ejabberd/devdoc/trunk/ejabberd_odbc.html