Does SystemVerilog support global functions? - function

I want to make a parity_check function which can be accessed by three different modules. Is this possible in SV? If yes, where do I declare this function and how do I import it into my module?

You can put a function into a separate file and include it using `include:
`include "some_file_name.sv"
However, a much better way is to use a package:
package some_package_name;
function string some_function_name;
return "some_function_name called";
endfunction
endpackage
You would put that into a separate file and you must compile that before compiling any module that uses it. You then import the package into each module:
module some_module_name;
import some_package_name::*; // or import some_package_name::some_function_name;
initial
$display(some_function_name);
endmodule
Putting a function in a package is better than just putting it into a file and using include, because a package is a named scope. Because a package is a named scope, any issues with some clash of names can be resolved by, instead of using import, referring to the full name of the function in its package, eg:
module some_module_name;
initial
$display(some_package_name::some_function_name);
endmodule

Related

Cython and Exec()?

If I made a python file named hello.py that has a script made like this.
msg = input("insert your message here: ")
script = '''
def say_something():
print("{msg}")
'''
exec(script)
say_something()
And then I tried to use Cython
from distutils.core import setup
from Cython.Build import cythonize
setup(
ext_modules=cythonize("Hello.py")
)
It will show an error like this: undeclared name not builtin: say_something
I do understand why this happens but I'm not really an expert with python and C just yet. This is just an example, but it's similar to what I'm trying to do with one of my projects. Is there any way I could resolve this? I want to find a way to convert the script string into C as well.
I was trying to build an editable python script.
Cython compiles the Python functions to a native binary that does what the CPython interpreter should do. exec is a function that execute arbitrary code at runtime (which is generally a very bad idea for speed, maintainability/readability and security). Cython does not support exec because it would mean that the could would be compiled at runtime. Thus, the code executed by exec cannot be a Cython code. However, the exec function can still be used to execute a pure-Python code. The error can be removed by turning off the Cython.Compiler.Options.error_on_unknown_names in the setup script (just before calling setup) as pointed out by #DavidW. With this Cython will not complain when it does not find a function defined by exec (or similar methods). Please keep in mind that CPython can only be used in this case instead of Cython (which partially defeat the purpose of using Cython in the first place).

Export objects and classes before their declaration makes them undefined

I try to repeat example from Mozilla Hacks (Export lists subtitle):
//export.js
export {detectCats, Kittydar};
function detectCats() {}
class Kittydar {}
//import.js
import {detectCats, Kittydar} from "./export.js";
console.log(detectCats); // function detectCats() {}
console.log(Kittydar); // undefined
Oops: Kittydar is undefined (btw., the problem is the same with simple Object).
But if I put export after Kittydar declaration it's ok:
//export.js
class Kittydar {}
export {Kittydar};
//import.js
import {Kittydar} from "./export.js";
console.log(Kittydar); // function Kittydar() {_classCallCheck(this, Kittydar);}
Is this a typo in the article?
I transpile this with babel and bundle with browserify. Then I include output bundle in a usual .html file (with <script> tag).
The standard is hard to follow on this, but the article is correct. This code works in es6draft and in the SpiderMonkey shell: both the function and the class are initialized by the time those console.log calls run.
Here's how it's supposed to work, in minute detail:
The JS engine parses import.js. It sees the import declaration, so then it loads export.js and parses it as well.
Before actually running any of the code, the system creates both module scopes and populates them with all the top-level bindings that are declared in each module. (The spec calls this ModuleDeclarationInstantiation.) A Kittydar binding is created in export.js, but it's uninitialized for now.
In import.js, a Kittydar import binding is created. It's an alias for the Kittydar binding in export.js.
export.js runs. The class is created. Kittydar is initialized.
import.js runs. Both console.log() calls work fine.
Babel's implementation of ES6 modules is nonstandard.
I think it's deliberate. Babel aims to compile ES6 modules into ES5 code that works with an existing module system of your choice: you can have it spit out AMD modules, UMD, CommonJS, etc. So if you ask for AMD output, your code might be ES6 modules, but the ES5 output is an AMD module and it's going to behave like an AMD module.
Babel could probably be more standard-compliant while still integrating nicely with the various module systems, but there are tradeoffs.

How do you namespace a Dart class?

How do you create a namespace for a Dart class? I come from a C# background, where one would just use namespace SampleNamespace { }.
How do you achieve the same in Dart?
Dart doesn't have the concept of namespaces, but instead it has libraries. You can consider a library to be sort of equivalent to a namespace, in that a library can be made of multiple files, and contain multiple classes and functions.
Privacy in Dart is also at the library, rather than the class level (anything prefixed with an underscore is private to that library).
An example of defining a library (using the example of a utilities library:
// utilities.dart
library utilities; // being the first statement in the library file
You can make other files part of the same library by using the part keyword. Part files are only used to help organize your code; you can put all your classes in a single library file, or split them among several part files (or part files and the library file) - it has no effect on the execution. It is stylistic to put the main library file in a parent folder, and part files in a src/ folder.
Expanding the example to show Part files.
// utilities.dart
library utilities;
part "src/string_utils.dart";
part "src/date_utils.dart";
Those part files then link back to the library they are part of by using the part of statement:
// src/string_utils.dart
part of utilities;
// functions and classes
String reverseString(s) => // implementation ....
String _stringBuilder(strings) => // a private (to the library) function,
// indicated by the leading underscore
//... snip other classes and functions
Now that you have a library containing a function, you can make use of that library elsewhere by importing the library:
// my_app.dart;
import "path/to/library/utilities.dart";
main() {
var reversed = reverseString("Foo");
// _stringBulider(["a","b"]); // won't work - this function is
// only visible inside the library
}
If you want to alias your library to avoid clashes (where you might import two libraries, both containing a reverseString() function, you use the as keyword:
// my_app.dart;
import "path/to/library/utilities.dart";
import "some/other/utilities.dart" as your_utils;
main() {
var reversed = reverseString("Foo");
var your_reversed_string = your_utils.reverseString("Bar");
}
The import statement also makes use of packages, as imported by pub, Dart's package manager, so you can also host your library on github or elsewhere, and reference your library as so:
// my_app.dart;
import "package:utilities/utilities.dart";
main() {
var reversed = reverseString("Foo");
}
The pub dependency is defined in a pubspec.yaml file, which tells pub where to find the library. You can find out more at pub.dartlang.org
It is important to note that only the library file can:
contain import statements. Part files cannot.
contain the library keyword. Part files cannot.
contain part files. Part files cannot.
One final point is that a runnable app file can (and is likely to be) a library file, and can also be made of part files
// my_app.dart;
library my_app;
import "package:utilities/utilities.dart";
part "src/edit_ui.dart";
part "src/list_ui.dart";
part "src/foo.dart";
main() {
var reversed = reverseString("Foo");
showEditUi(); // perhaps defined in edit_ui.dart....?
}
The easiest way that I've found to create a namespace in Dart is this:
Say you have the files a.dart and b.dart containing the classes Apple and Banana respectively. Create a file called my_namespace.dart. In this example, it's residing in the same folder as the other two files. Export all the files that you want under your namespace from the my_namespace.dart file:
export 'a.dart';
export 'b.dart';
Then wherever you would like to use the exported code from these two files, use this:
import 'my_namespace.dart' as my_namespace;
// you can now access the classes under the same namespace:
final myApple = my_namespace.Apple();
final myBanana = my_namespace.Banana();
Another way to do this, which removes the need of the intermediary file my_namespace.dart, is to have several import statements with the same alias:
import 'a.dart' as my_namespace;
import 'b.dart' as my_namespace;
// you can once again access the classes under the same namespace:
final myApple = my_namespace.Apple();
final myBanana = my_namespace.Banana();
I prefer the first method because I don't have to repeat the multiple import statements whenever I need to use a class under the namespace.
Of course the imported and exported files do not need to be in the same folder, but having the files under the same namespace in the same folder would probably be more convenient.

how to force compiler compile all classes in my project?

im using Adobe® Flash® Builder™ 4.6,the problem also exist in previous versions.
for some reason ,i am using
cls = applicationDomain.getDefinition(name) as Class;
to get the object's constructor and then create the instance of my modules class.thus make compile ignore my module classes ,because they are not related from my main class.how to force else classes also compiled into my swf or swc file? i didn't find where i can adjust my compile option.
by now i use this way to solve my problem,at the very beginning of the program entry.
if(1+1==3){
//never be run but do make classes merge into swf files.
new MyModule();
}
i have hundreds of modules like this one,i do hope i can find a way to solve this problem permanently
You can try with this
package
{
public class IncludeClasses
{
import com.abc.db.Database;Database;
import com.abc.logs.RemoteLogTarget; RemoteLogTarget;
import com.abc.logs.LocalLogTarget; LocalLogTarget;
import com.abc.exception.GlobalExceptionHandler; GlobalExceptionHandler;
import com.abc.utils.NetConnectionMonitor;NetConnectionMonitor;
}
}
You need to use the class to get it to compile in the swf.
Not the best method but
private var someVar:someClass;
Using the "new" keyword will cause the run-time to allocate memory for the object so you don't want to use that.
This whole loading modules and compiling classes has a code smell to it.
You would be better off having your classes in the modules implement an interface.
You need at least one strict reference to your class to appear within the project. I use a static variable of type Array to stuff all of the classes I need, and never really reference that array, if I can.
private static var dummy:Array=[OneClass, AnotherClass, Class01, Etc];
You can also do this by setting your compiler flag.
About the application compiler options
See:
include-libraries library [...]
Include only classes that are inheritance dependencies of classes that
are included with the include-classes compiler option.
The default value is false.
This is an advanced option. You might use this compiler option if you
are creating a custom RSL and want to externalize as many classes as
possible. For example:
compc -include-classes mx.collections.ListCollectionView
-include-inheritance-dependencies-only=true
-source-path . -output lcv2 -directory

Running a specific Clojure namespace-function?

Is there a way to cause the clojure-launcher (i.e. the wrapper around clojure.main) to run a specific function from a certain namespace (I'm looking for a solution that does not require AOT-compiling the namespace and calling its main-function)?
You can call namespace's main function (or any other function) withtout AOT just pipe,
(use 'ns)
(fn)
to clojure.main assuming classpath stuff set up correctly.