I need to create a secure constructor, which only builds the object if the values are correct, so I want to return null in the constructor (not to create an instance of the object) how to do this?
In my class Aresta null return from constructor isn't works;
export class Aresta{
private feromonioAtual : number;
private distancia : number;
private noFim: No;
constructor(noFim:No, distancia:number){
if(distancia<=0){
//null return isn't work :(
return null;
}
this.feromonioAtual = 1;
this.noFim = noFim;
this.distancia = distancia;
}
}
throwan error from the constructor to let it tell the code that created the object that something is wrong; this doesn't prevent or cancel the creation of the object; the object is created but incompletely initialized; the code that creates the object should handle the error.