Time for action—A fridge with constraints

Therefore, now our fridge will only be able to access eatable items.

  1. First, create a parameterized Fridge class that will only accept types implementing the Eatable interface:
    class Fridge<T: Eatable)>
    {
    public function new()
    {}
    }
    
  2. In this class, we will create two methods: an add and a remove method and we will use an Array to store the list of what is inside our Fridge:
    class Fridge<T: Eatable)>
    {
    private var items : Array<T>; public function new()
    {
    items = new Array<T>(); //Initialize the Array
    }
    public function add(item : T)
    {
    array.push(item);
    }
    public function remove(item : T)
    {
    array.remove(item);
    }
    }
    

    For sure, in this state, our fridge is basically just a wrapper around the array class, but now we will do something interesting with it.

  3. We will define the Eatable interface, so that we can store the date before which the eatable should be eaten, as follows:
    interface Eatable
    {
    public var eatBefore : Date;
    }
    
  4. Now, add a method to our fridge to dump all eatables that shouldn't be eaten anymore, as follows:
    public function removeOldItems()
    {
    for(eatable in items)
    {
    if(eatable.eatBefore.getTime() <Date.now().getTime()) //Should've been eaten earlier
    {
    this.remove(eatable); } } }
    

What just happened?

In the preceding Time for action, we have put to practice what we have seen in this chapter about type parameters and constraints.

  • Creating a parameterized class: We have created the parameterized Fridge class, therefore allowing us to add and remove items to and from it, still knowing that they are of a specific type (T can represent any type extending or implementing Eatable, or even just Eatable).
  • Adding constraint: By putting the constraint on the abstract type T, we knew that all objects of this abstract type would be of type Eatable. This allowed us to use the public field defined in the Eatable interface.

Have a go hero—Creating a typed container

Try to create a container for three values (we will create a class with first, second, and third fields to store these values).

These values should be typed, but the class should be able to store any type of values (these should all be of the same type).

Pop quiz—Verify your knowledge

  1. A value can have:

    a. One type

    b. Two types

    c. Three types

    d. As many types as we want

  2. The safe way to do some casting is:

    a. cast (variable, NewType)

    b. cast variable

    c. untyped { var var2: NewType = variable;}

  3. A variable can be typed (2 good answers):

    a. Explicitly

    b. Through inference

    c. Untyped

  4. An abstract type can have:

    a. No constraints at all

    b. One constraint at most

    c. Two constraints at most

    d. An unlimited number of constraints

  5. A parameterized class with the abstract type T can access methods of objects of type T even if no constraints were put on T:

    a. True

    b. False

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.31.165