📚Libraries

Corgi also allows you to use mixins from other files through the use operator. It is similar to an import, but for mixins.

If you use a directory of corgi files, you can make their exported mixins available in your file.

Just like imports, each time you want to use a mixin from a used file or directory, you must prefix your mixin call with the namespace of the used library.

used files may only use other files, import packages, define top-level code, and define mixins.

Instead of the regular .corgi file extension, library files use the .corgil extension.

github.com/me/proj/bar/foo.corgil
- name := "Foo"

mixin Foo()
  > #{name} did it!
use "github.com/me/proj/bar"

p: +bar.Foo()

Use Aliases

Just like imports, you can also define aliases for uses:

use baz "github.com/me/proj/bar"

p: +baz.Foo()

Top-Level Code

Top-Level code in a library executed at the beginning of the function. Symbols defined in it are accessible to all mixins in that library, but not outside of the library.

The corgi:formixin compiler directive

Using the corgi:formixin compiler directive, you can tie a top-level code block to certain mixins. That way they are only included in the generated function, if the mixin is actually called.

This is necessary, since the Go compiler will error if you have unused variables, and if you don't use a mixin, corgi won't include it in the generated function.

Example

Corgi's stdlib has a great example for the formixin directive in the fmt package:

//corgi:formixin Num SetNum
- numSep := " "

mixin Num(val any, sep=numSep, ...)
  // ...
  
mixin SetNumSep(sep string)
  - numSep = sep

Directory Libraries

Each file also has access to its directory library. The directory library is a library of all library files found in the same dir as the file being compiled.

Last updated