I am just starting to learn Scala and was following this tutorial. They implement a Tree structure as the code below shows to create mathematical formula's. Halfway through they introduce the type keyword as
type Environment = String => Int
so that all variables can be mapped to numbers.
My question is, how do I refer to the type without having an instance of Tree? I.e. how can I define the type Environment as static.
Example code:
package com.company
/**
  * The tree class which represents a formula
  */
abstract class Tree {
    /**
      * The type to map a variable to a value
      */
    type Environment = String => Int
    /**
      * Using the given Environment, it evaluates this tree
      * @param env The environment which maps variables to values
      * @return The result of the fomula
      */
    def eval(env: Environment): Int = this match {
        case Sum(lhs, rhs) => lhs.eval(env) + rhs.eval(env)
        case Variable(v)   => env(v)
        case Constant(c)   => c
    }
}
/**
  * Represents a sum between to values
  * @param lhs Left hand value
  * @param rhs Right hand value
  */
case class Sum(lhs: Tree, rhs: Tree) extends Tree
/**
  * Represents an unknown and named value or named variable
  * @param variable The name of the variable
  */
case class Variable(variable: String) extends Tree
/**
  * An unnamed constant value
  * @param value The value of this constant
  */
case class Constant(value: Int) extends Tree
/**
  * Base class for this program
  */
object Tree {
    /**
      * Entry point of the application
      * @param args
      */
    def main(args: Array[String]) {
        //Define a tree: (x + 3) + 2
        val tree: Tree = Sum(Sum(Variable("x"), Constant(3)), Constant(2))
        //Define an environment: x=5 and y=6
        val env: tree.Environment = {case "x" => 5; case "y" => 6}
        //       ^ Refers to the object tree rather than the class Tree
        //val env: Tree.Environment = {case "x" => 5; case "y" => 6}
        //  ^ Results in the error: Error:(55, 27) type Environment is not a member of object com.company.Tree
        println(tree) //show the tree
        println(s"x=${env.apply("x")} | y=${env.apply("y")}") //show values of x and y
        println(tree.eval(env)) //evaluates the tree
    }
}
 
     
    