Search code examples
wren

How do I write the equals (==) function?


class Character {
  
    construct new(name,life){
        _name = name
        _life = life
    }
    
    name  { _name }
    life  { _life }
}

If the name and life are the same, it should return true otherwise false.


Solution

  • class Character {
      
        construct new(name,life){
            _name = name
            _life = life
        }
        
        name  { _name }
        life  { _life }
        
        ==(other){
            return _name == other.name && 
                   _life == other.life
        }
    }
    
    var player_one = Character.new("Nova",100)
    var player_two = Character.new("Mint Hot Chocolate",50)
    var player_three = Character.new("Nova",100)
    
    System.print(player_one == player_three) // Output: true
    System.print(player_one == player_two) // Output: false