Skip to content

03. Guards

Pete Shand edited this page Aug 3, 2018 · 1 revision

Guards say "yes" or "no". They are typically used by extensions to guard against certain actions based on environmental conditions.

Guard Forms

A guard can exist in one of three forms:

  • Function
  • Object
  • Class

Function Guards

A function guard is expected to return a Boolean value:

function randomGuard():Bool {
   return Math.random() < 0.5;
}

Object Guards

An object guard is expected to expose an "approve" method that returns a Boolean value:

class HappyGuard
{
   public function approve():Bool
   {
      return true;
   }
}

Class Guards

Instantiating a Class guard should result in an object that exposes an "approve" method which returns a Boolean value:

class SomeGuard
{
   @inject public var someModel:SomeModel;

   public function approve():Bool
   {
      return someModel.enabled;
   }
}
Clone this wiki locally