Wiki
Version 2 (modified by hopscc, 15 years ago)

--

Use Directive

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

Specify a namespace contents to make 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 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.Lang

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.Lang.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 namespace in Foo.Bar.dll if not already available

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

With MyProg.cobra containing

...
    use Foo.Bar
...

Instead of compilation by

cobra -reference:Foo.Bar MyProg.cobra

you can just say:

cobra MyProg.cobra