Version 9 (modified by nerdzero, 10 years ago) |
---|
Use Directive
use [ <alias> = ] <namespace> [ from <library-name> ] use <namespace> use <namespace> from <library-name> use <alias> = <namespace>
Specify a namespace whose contents are made available to this module.
When using a form without a 'from' clause, if the compiler cannot immediately locate the namespace,
i.e if its not in the default loaded libraries,
The compiler will look for a (platform dependent) library-name of the same form as the namespace.
If the filename of the library differs from the namespace, you can specify it with the second form.
<library-name> can be a simple identifier, qualified identifier (Foo.Bar) or a string literal.
use Foo.Bar from SomeLib
You can put single or double quotes around the file name if its components are not legal identifiers
(for example, the filename has a space or punctuation mark in it).
use Foo.Bar from "My Lib"
The alias clause (third form above) can be used where you want to use a different (or simplified) name for the namespace than that provided.
This is useful for a lengthy fully qualified namespace name that is widely used.
Platform
For .Net: There are assumed five implicit "use" directives at the top of a Cobra program:
use System use System.Collections.Generic use System.IO use System.Text use Cobra.Core
This gives default access to the core of the .Net objects and the cobra runtime Library, CobraCore.
These correspond to access to the following default loaded libraries
mscorlib.dll System.dll Cobra.Core.dll
On .Net <library-name> will refer to a .Net Assembly or dll
No ".dll" extension is expected (or allowed) in any of the above syntax. The library name is the namespace with ".dll" appended e.g
use Foo.Bar # On .Net will look for the namespace in file Foo.Bar.dll if it is not already available
For Java: The namespace names specified refer to java package names (first (caps) letter downcased).
Similarly the assumed implicit directives are
use Java.Lang use Java.Io use Java.Util use Java.Text use Cobra.Core
which correspond to the most common core java library packages (i.e java.{lang,io,util,text}) and the (java ) cobra runtime library. (These core library packages are read from the standard java runtime system jarfile rt.jar).
The <library-name> refers to a Java jar file (no .jar extension is expected or allowed).
The jarfile name is the namespace with ".jar" appended searched for in the places given by the CLASSPATH env variable.
Examples
use System.Windows.Forms use System.Drawing class MyForm inherits Form def init .text = 'Click Me' listen .click, ref .handleClick def handleClick(sender as Object, args as EventArgs) MessageBox.show('You clicked me!', 'Click') class Program def main is shared Application.run(MyForm())
Aliasing .Net namespaces to something more convenient
use Cons = System.Console use CC = Cobra.Core.CobraCore class Test def main is shared Cons.writeLine('Display Version') ver = CC.version v1 = Cobra.Core.CobraCore.version assert ver == v1 Cons.writeline(ver)
Aliasing for shortening names
namespace PC # Define an alias for the nested namespace. use Project = PC.MyCompany.Project class A def main is shared # Use the alias mc = Project.MyClass() print mc namespace MyCompany namespace Project class MyClass pass
Notes
In most cases this syntax means you dont need to specify the commandline -reference switch to provide the filename for a referenced namespace if the namespace dll follows usual naming conventions.
With MyProg.cobra containing
... use Foo.Bar ...
Instead of compilation by
cobra -reference:Foo.Bar MyProg.cobra
you can just say:
cobra MyProg.cobra
If the dll filename containing the namespace code differs from the namespace name you can wire the namespace/filename relationship into the source file rather than having to always remember (or script) a specific separate compiler commandline reference..
e.g Assuming a class Augmented.ExtendedSys.ExtConsole kept in a utility dll (AugmentationLibrary.dll) say
rather than program MyProg.cobra containing something like
... use Augmented.ExtendedSys ... # use as input = ExtConsole.input('Enter text:')
and compilation commandline
cobra -ref:AugmentationLibrary MyProg.cobra
you can just have:
... use Augmented.ExtendedSys from AugmentionLibrary ...
and use a non -ref compilation invocation.
cobra MyProg.cobra
Here (where the name is a little cumbersome) for more convenience use both features...
... use AugCons = Augmented.ExtendedSys.ExtConsole from AugmentationLibrary ... # use as input = AugCons.input('Enter text:')