Adding members to a class

Parcel is a small class with one field, weight. Right-click its name line and the editor offers to add the members a class can have: a field, a constructor, a method, an operator. Each item writes the code in the right place, in the right order, and is refused if it would leave an error. To play it in the IDE: Help > Lessons > Writing code…, or ek9 -ide -lesson adding-members.

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.addingmembers

  defines class
    Parcel
      weight as Integer: Integer()

      default operator ?

  defines program
    ShowParcel()
      stdout <- Stdout()
      parcel <- Parcel()
      stdout.println(`Parcel set? ${parcel?}`)

//EOF

Step 1: A field

Right-click the Parcel line and choose Add Field…

The EK9 IDE once this is done: Right-click the Parcel line and choose Add Field…

Type label as String: String() - a name, a type, and the unset value it starts with. Choose OK.

The EK9 IDE just before choosing OK: Type label as String

The preview shows label joining weight, with the other members left where they were. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows label joining weight, with the other members left where they were

Step 2: A constructor

Right-click the Parcel line and choose Add Constructor > From All Fields (pure). The submenu also offers a no-arg constructor, private or pure - and only the ones that are still missing.

The EK9 IDE once this is done: Right-click the Parcel line and choose Add Constructor > From All Fields (pure)

Step 3: A method

Right-click the Parcel line and choose Add Method…

The EK9 IDE once this is done: Right-click the Parcel line and choose Add Method…

describe is the method's name, String the return type, and pure ticked - it only reads the fields. Fill them in and choose Apply.

The EK9 IDE just before choosing Apply: describe is the method's name, String the return type, and pure ticked - it only reads the fields

Step 4: An operator

Right-click the Parcel line and choose Add Operator > $ (string) > skeleton. The other choice, default, would write 'default operator $' - the compiler's own version, with no body to read.

The EK9 IDE once this is done: Right-click the Parcel line and choose Add Operator > $ (string) > skeleton

Step 5: Back to a default operator

Put the caret on the operator $ line - the skeleton just written spells out exactly what the compiler would generate.

The EK9 IDE once this is done: Put the caret on the operator $ line - the skeleton just written spells out exactly what the compiler would generate

Right-click it and choose Collapse to 'default operator $'.

The EK9 IDE once this is done: Right-click it and choose Collapse to 'default operator $'

Choose File > Save.

The EK9 IDE once this is done: Choose File > Save

The result

That is the right-click menu on a class: Parcel gained a field, a constructor, a method and an operator without a line typed, and each item refused anything that would have left an error. The describe method is only a stub (//AI: Implement describe) - its logic is yours, or an AI assistant's, to write. Collapse to 'default operator' undid the explicit $ because its body matched what the compiler would generate; the opposite direction, Expand 'default operator $' to Skeleton, is the expand-default-operator lesson. AI assistants use ek9_add_field, ek9_add_constructor, ek9_add_method, ek9_add_operator, ek9_collapse_to_default_operator and ek9_expand_default_operator.

Where this lesson comes from