Declaring and assigning variables

The complete source of basics_variables.ek9, compiled by the EK9 compiler. Shown on basics.html.

#!ek9
defines module site.basics.variables

  defines function

    inferredDeclaration()
      stdout <- Stdout()

      //Declare an Integer - but with no value yet known.
      age <- Integer()
      //Declare an Integer - but with a known value from the outset.
      minimumAge <- 21

      stdout.println(`${age?} ${minimumAge}`)

    explicitDeclaration()
      stdout <- Stdout()

      //Declare an Integer - but with no value yet known.
      age as Integer: Integer()
      //Or like this.
      ageAgain as Integer = Integer()
      //Or even like this.
      ageOnceMore as Integer := Integer()
      //Declare an Integer - without type inference
      minimumAge as Integer := 21

      stdout.println(`${age?} ${ageAgain?} ${ageOnceMore?} ${minimumAge}`)

    laterAssignedDeclaration()
      stdout <- Stdout()

      //Declare an Integer - but no space allocated
      possibleError as Integer?

      possibleError: 42
      stdout.println(possibleError)

    preferredForms()
      stdout <- Stdout()

      minimumAge <- 21
      //Alternatively - which can be useful
      minimumAgeTyped as Integer: 21

      stdout.println(`${minimumAge} ${minimumAgeTyped}`)

    unsetIsNotExecuted()
      stdout <- Stdout()

      age <- Integer()
      //age exists, but has no meaningful value yet - so 'age?' is false
      stdout.println(`age is set: ${age?}`)

      age: 21
      //Now it has one - so 'age?' is true
      stdout.println(`age is set: ${age?}`)

    collectionIsSet()
      stdout <- Stdout()

      //A simple empty list
      aList as List of Integer := List()
      if aList?
        //Code where aList has some meaningful value/content
        stdout.println("aList is set, even though it is empty")

      //A list with some values using the 'list shorthand of' [] - this not an array!
      bList <- [ "A", "B", "C" ]

      stdout.println(bList)

    iterating()
      stdout <- Stdout()
      bList <- [ "A", "B", "C" ]
      iteratorB <- bList.iterator()
      while iteratorB?
        stdout.println(`Value is [${iteratorB.next()}]`)

      //The stream pipeline construct is cleaner though
      cat bList > stdout

    reassignment()
      stdout <- Stdout()

      //Declare charges and initialise
      charges <- 20.99
      //Use charges for some processing
      stdout.println(charges)
      //Now set the same variable to a new value
      charges := 50.99
      //Use updated charges for some processing

      stdout.println(charges)

    assignmentForms()
      stdout <- Stdout()

      //Declare charges and initialise
      charges <- 20.99
      stdout.println(charges)
      charges: 50.99
      charges = 50.99
      //Using := 'feels' more significant
      charges := 50.99

      stdout.println(charges)

    comments()
      stdout <- Stdout()

      //A one line comment
      variable <- 21 //Comments can be at the end of a line if necessary

      <!--
      These lines will be contained
      within a comment block.
      -->

      <!-
      These lines will be contained
      within a comment block, just like above.
      -!>

      stdout.println(variable)

    <?-
    Just sums the squares of values from 1 to n.
    -?>
    sumOfSquares()
      -> n as Integer
      <- sum as Integer: 0
      for i in 1 ... n
        sum += i * i

  defines program

    Variables()
      inferredDeclaration()
      explicitDeclaration()
      laterAssignedDeclaration()
      preferredForms()
      unsetIsNotExecuted()
      collectionIsSet()
      iterating()
      reassignment()
      assignmentForms()
      comments()
      Stdout().println(sumOfSquares(3))

Download basics_variables.ek9

Output

false 21
false false false 21
42
21 21
age is set: false
age is set: true
aList is set, even though it is empty
["A", "B", "C"]
Value is [A]
Value is [B]
Value is [C]
A
B
C
20.99
50.99
20.99
50.99
21
14