charly-vm icon indicating copy to clipboard operation
charly-vm copied to clipboard

Export statement

Open KCreate opened this issue 3 years ago • 0 comments

Exporting variables, functions, classes

  • the export keyword can be used to export constants, functions and classes from a module
  • the compiler desugars the export statements into a class declaration that gets returned from the module function
  • exporting let declarations is prohibited, as to not cause the possible misconception that any future updates to the variable will be reflected in the module instance
export const bar = 25

export func do_stuff {
  print("hi")
}

export class Person {
  property name
  property age
}

export foo
export foo.baz

Default export

  • A module can export at most one default export
  • The default export is the binding that is used when no specific named bindings are imported
// liba.ch
export const foo = 100
export class Person {}
export default class FooBar {}



// main.ch
import liba
liba      // class FooBar

import liba as mylib
mylib     // class FooBar

import { foo, Person, FooBar } from liba
foo       // 100
Person    // class Person
FooBar    // class FooBar
liba      // class FooBar

import { foo, Person, FooBar } from liba as mylib
foo       // 100
Person    // class Person
FooBar    // class FooBar
liba      // class FooBar
mylib     // class FooBar

KCreate avatar Apr 03 '22 00:04 KCreate