New Construct: the dialogs

Fleet is a module with one small program in it. Code > New Construct writes a new class, record, trait, function, program or other construct into the open file - a dialog asks for what that kind needs, Preview shows the text, Apply writes it. To play it in the IDE: Help > Lessons > Writing code…, or ek9 -ide -lesson new-constructs.

The EK9 IDE as the lesson opens

Start from

The code the lesson starts from: copy it into a file of your own and follow the steps.

#!ek9
defines module lesson.newconstructs

  defines program
    ShowFleet()
      stdout <- Stdout()
      stdout.println("Fleet ready")

//EOF

Step 1: A class that may be extended

Choose Code > New Construct > Class…

The EK9 IDE once this is done: Choose Code > New Construct > Class…

Vehicle is the class's name. Closed is already selected - EK9's default, a class nobody can extend. Select Open instead, so Vehicle can be a parent, and choose Preview.

The EK9 IDE just before choosing Preview: Vehicle is the class's name

The preview shows Vehicle as open, in a defines class block of its own. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Vehicle as open, in a defines class block of its own

Step 2: A class that extends it

Choose Code > New Construct > Class…

The EK9 IDE once this is done: Choose Code > New Construct > Class…

Truck is the name. Type Vehicle into the Extends: field - the field turns red for a type that does not exist, and Preview stays grey until it does. Leave Closed selected and choose Preview.

The EK9 IDE just before choosing Preview: Truck is the name

The preview shows Truck extends Vehicle. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Truck extends Vehicle

Step 3: A trait

Choose Code > New Construct > Trait…

The EK9 IDE once this is done: Choose Code > New Construct > Trait…

Describable is the trait's name. There is no Closed, Open or Abstract choice and no Extends: field here - a trait is a contract, never extended the way a class is. Type Describable and choose Preview.

The EK9 IDE just before choosing Preview: Describable is the trait's name

The preview shows Describable in a defines trait block. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Describable in a defines trait block

Step 4: A package

Choose Code > New Package…

The EK9 IDE once this is done: Choose Code > New Package…

A package block holds the module's version, description and what to include. Type a description, tick Apply standard includes so every .ek9 file in the tree is part of the package, and choose Create.

The EK9 IDE just before choosing Create: A package block holds the module's version, description and what to include

The result

That is New Construct: every dialog asks for a name and the few choices that kind of construct has - Closed, Open or Abstract and an Extends: for a class, none of either for a trait - then Preview shows the exact text before Apply writes it, and Apply is refused if it would leave an error. Record… (with its Fields table), Function… (parameters, return, pure), Type ▸, Constant…, Program… and Test… work the same way - the Getting Started lessons use them. New Package… writes a package block into the open file. AI assistants use ek9_new_construct (and ek9_add_program for a program or test).

The EK9 IDE when the lesson has finished

Where this lesson comes from