Move Construct

Move Construct moves a class, record, trait, component or function to another file - into that file's module, when it is another - and every reference to it follows. Here priceLabel is about money but sits in the shop's module, lesson.shop; money.ek9, in lesson.money, is where it belongs. To play it in the IDE: Help > Lessons > Refactoring…, or ek9 -ide -lesson move-construct.

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.

shop.ek9

#!ek9
defines module lesson.shop

  references
    lesson.money::withVat

  defines function

    priceLabel() as pure
      -> amount as Float
      <- rtn as String: `GBP ${amount}`

  defines program

    Shop()
      stdout <- Stdout()
      stdout.println(`Ten with VAT is ${priceLabel(withVat(10.0))}`)

//EOF

money.ek9

#!ek9
defines module lesson.money

  defines constant
    VAT_RATE <- 1.2

  defines function

    withVat() as pure
      -> amount as Float
      <- rtn as Float: amount * VAT_RATE

//EOF

Step 1: Where it is

Put the caret on priceLabel, on line 9.

The EK9 IDE once this is done: Put the caret on priceLabel, on line 9

Step 2: Move Construct

Choose Refactor > Move Construct…

The EK9 IDE once this is done: Choose Refactor > Move Construct…

Move to file is money.ek9 - in lesson.money, so the move is cross-module. Choose Preview.

Step 3: Preview and apply

The preview shows every change the refactoring makes; then the IDE formats each file it changed. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows every change the refactoring makes; then the IDE formats each file it changed

The result

That is Move Construct: priceLabel now lives in money.ek9, in lesson.money, and shop.ek9 references it as lesson.money::priceLabel, beside withVat. A move within one module changes no reference at all. F6 opens it; AI assistants use the ek9_move_construct tool.

The EK9 IDE when the lesson has finished

Where this lesson comes from