I am facing an issue where I need to handle multiple values and ensure that only unique ones are used. With the use of node js and access to harmony collections through the --harmony flag, I have considered using a Set as a potential solution.
What I am seeking is something akin to the scenario illustrated below:
'use strict';
function Piece(x,y){
this.x = x;
this.y = y;
}
function Board(width,height,pieces){
this.width = width;
this.height = height;
this.pieces = pieces;
}
function generatePieces(){
return [
new Piece(0,0),
new Piece(1,1)
];
}
//boardA and boardB represent two distinct yet comparable boards
var boardA = new Board(10,10,generatePieces());
var boardB = new Board(10,10,generatePieces());
var boards = new Set();
boards.add(boardA);
boards.has(boardB); //returns true
In another programming language like c#, it would typically be necessary to implement an equals function and a hash code generation function for both Board and Piece classes to achieve similar functionality. This is because default object equality is usually based on references. Alternatively, utilizing a special immutable value type (e.g., a case class in scala) could also be considered.
Is there a way to define equality for my objects in order to address this issue?