Chapter 38: a library to depend on, built and installed

The chapter's consumer needs a library to depend on, and a dependency can only come from a library on your own disk today, so it must be installed first. This one is tiny - a constant and two area functions - and its package block lists nothing it needs to declare: no program, no gated type, so no capabilities. The IDE writes the constant and the functions; you type the manifest and the two claims; Build packages and installs it into a library made for this lesson. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-package-shapes.

The EK9 IDE as the lesson opens

What you'll build

The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.

shapes/shapes.ek9

#!ek9
defines module book.shapes.lib

  defines constant
    CIRCLE_CONSTANT <- 3.14159

  defines function

    circleArea() as pure
      -> radius as Float
      <- rtn as Float: CIRCLE_CONSTANT * radius * radius

    squareArea() as pure
      -> side as Float
      <- rtn as Float: side * side

  defines package
    publicAccess <- true
    version <- 1.0.0-0
    description <- "Area of simple shapes"
    license <- "MIT"
    applyStandardIncludes <- true

shapes/dev/shapesTests.ek9

#!ek9
defines module book.shapes.lib.tests

  references
    book.shapes.lib::circleArea
    book.shapes.lib::squareArea

  defines program

    @Test
    CircleAreaTest()
      assert circleArea(1.0) == 3.14159

    @Test
    SquareAreaTest()
      assert squareArea(3.0) == 9.0

Step 1: A constant

Choose Code > New Construct > Constant…

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

CIRCLE_CONSTANT is the constant's name and 3.14159 its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.

The EK9 IDE just before choosing Preview: CIRCLE_CONSTANT is the constant's name and 3.14159 its value - a literal in the Value field, from which the type is inferred

The preview shows CIRCLE_CONSTANT about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows CIRCLE_CONSTANT about to be written

Step 2: circleArea — a pure function

Choose Code > New Construct > Function…

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

circleArea is the function's name. Add one parameter, radius of type Float, set Returns to Float and tick pure. Choose Preview.

The EK9 IDE just before choosing Preview: circleArea is the function's name

The preview shows circleArea about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows circleArea about to be written

Select the circleArea() as pure line and, above it, type the blank line the book opens its first function with. Saves when typed.

The EK9 IDE once this is done: Select the circleArea() as pure line and, above it, type the blank line the book opens its first function with

Select the stub, from <- rtn as Float? down to its //AI line, and type circleArea's own return and work, as the book writes them. Saves when typed.

The EK9 IDE once this is done: Select the stub, from <- rtn as Float? down to its //AI line, and type circleArea's own return and work, as the book writes them

Step 3: squareArea — a pure function

Choose Code > New Construct > Function…

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

squareArea is the function's name. Add one parameter, side of type Float, set Returns to Float and tick pure. Choose Preview.

The EK9 IDE just before choosing Preview: squareArea is the function's name

The preview shows squareArea about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows squareArea about to be written

Select the stub, from <- rtn as Float? down to its //AI line, and type squareArea's own return and work, as the book writes them. Saves when typed.

The EK9 IDE once this is done: Select the stub, from <- rtn as Float? down to its //AI line, and type squareArea's own return and work, as the book writes them

Step 4: The manifest — defines package

Where Maven keeps a pom.xml and npm a package.json, EK9 keeps the manifest in the source: a defines package block at the end of the module, the version in it a native Version literal, no second file to disagree with it. Select the last line of the code and, after it, type the block. Saves when typed.

The EK9 IDE once this is done: Where Maven keeps a pom.xml and npm a package.json, EK9 keeps the manifest in the source

Step 5: The tests' file

Open shapes/dev/shapesTests.ek9 - already there, with just its own module line. Packaging refuses a project whose tests fail or cover less than 80%, so they come before the package.

The EK9 IDE once this is done: Open shapes/dev/shapesTests.ek9 - already there, with just its own module line

Select the module line, and below it type a references block bringing in what the tests call. Saves when typed.

The EK9 IDE once this is done: Select the module line, and below it type a references block bringing in what the tests call

Step 6: CircleAreaTest

Choose Code > New Construct > Test…

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

CircleAreaTest is the test's name. Choose Preview.

The EK9 IDE just before choosing Preview: CircleAreaTest is the test's name

The preview shows CircleAreaTest about to be written, marked @Test, with a placeholder assertion. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows CircleAreaTest about to be written, marked @Test, with a placeholder assertion

Select the @Test line, and above it type the blank line every chapter example opens its first member with. Saves when typed.

The EK9 IDE once this is done: Select the @Test line, and above it type the blank line every chapter example opens its first member with

Select the stub, from result <- "TODO: assert the unit under test" down to its //AI line, and type the real claim: a circle of radius one has the constant for its area. Saves when typed.

The EK9 IDE once this is done: Select the stub, from result <- "TODO

Step 7: SquareAreaTest

Choose Code > New Construct > Test…

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

SquareAreaTest is the test's name. Choose Preview.

The EK9 IDE just before choosing Preview: SquareAreaTest is the test's name

The preview shows SquareAreaTest about to be written, marked @Test, with a placeholder assertion. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows SquareAreaTest about to be written, marked @Test, with a placeholder assertion

Select the stub, from result <- "TODO: assert the unit under test" down to its //AI line, and type the real claim: a square of side three has area nine. Saves when typed.

The EK9 IDE once this is done: Select the stub, from result <- "TODO

Step 8: Run the tests

Run ▸ Run Tests (⌘T) runs every @Test in the project - the same tests -P will run before it packages anything. Both tests pass. Choose Run Tests.

The EK9 IDE once this is done: Run ▸ Run Tests (⌘T) runs every @Test in the project - the same tests -P will run before it packages anything

Step 9: Package

Choose Build ▸ Package….

The EK9 IDE once this is done: Choose Build ▸ Package…

The report says Success, with nothing more to say: the module compiled, its two tests passed and the archive and checksum were written. Choose Close.

Step 10: Install

Choose Build ▸ Install Package.

The EK9 IDE once this is done: Choose Build ▸ Install Package

Install copied the archive into the library, depth-package-shapes-home/.ek9/lib here - ~/.ek9/lib for you outside this lesson - where another project's deps entry would find it. Choose Close.

The result

shapes.ek9 and dev/shapesTests.ek9 now read exactly as chapter 38 of EK9 in Depth shows them, and book.shapes.lib-1.0.0-0 sits in the lesson's own library. A consumer would name it in a deps entry and the resolver would find the archive there, compiling its source with the consumer's. Your own ~/.ek9 was never touched.

The EK9 IDE when the lesson has finished

Where this lesson comes from