Wiki
Version 6 (modified by hopscc, 11 years ago)

--

Use Directive

use <namespace>
use <namespace> from <library-name>

Specify a namespace whose contents are made available to this module.

When using the first form if the compiler cannot immediately locate the namespace,
i.e if its not in the default loaded libraries,
it 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"

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())

Notes

In most cases this syntax means you dont need to specify the commandline -reference switch to provide the filename for a referenced namespace.

With MyProg.cobra containing

...
    use Foo.Bar
...

Instead of compilation by

cobra -reference:Foo.Bar MyProg.cobra

you can just say:

cobra MyProg.cobra