0

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;
    }
}
3
  • 1
    what you are looking for is the factory pattern. You need an object that creates instances of Aresta and checks if all the data is there. Commented May 14, 2018 at 12:14
  • Throw where you'd return null and handle exceptions outside the class, i.e. where you'd instantiate. Instances whose constructor threw will remain undefined. Commented May 14, 2018 at 12:16
  • 1
    1. constructors don't return values (or, better said, the values they return are ignored). 2. when the constructor is invoked the object has already been created and there is no way the constructor can prevent the creation; the role of the constructor is to prepare the object for work (initialize it); 3. you can throw an 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. Commented May 14, 2018 at 12:21

2 Answers 2

1

This kind of validation is handled outside the class in a factory.

export class Aresta {
    private feromonioAtual: number;
    private distancia: number;
    private noFim: number;

    constructor(noFim: number, distancia: number) {
        this.feromonioAtual = 1;
        this.noFim = noFim;
        this.distancia = distancia;
    }
}

export class ArestaFactory {
    public static buildAresta(noFim: number, distancia: number): Aresta {
        if (distancia <= 0) {
            return null; // or throw an exception
        }
        return new Aresta(noFim, distancia);
    }
}

Sadly until now there is no package/module private accessor in typescript but a ticket about it is open in their github here.

Sign up to request clarification or add additional context in comments.

Comments

1

I would use a factory method for this, which allows you to hide the constructor from other code. It can return either null or an instance of Aresta and can contain any validation required to check the construction values.

export class Aresta{
    private feromonioAtual : number;
    private distancia : number;
    private noFim: No;

    public static GetInstance(noFim: No, distancia: number) : Aresta | null {
        if(distancia <= 0){
            return null;
        }

        return new Aresta(noFim, distancia);
    }

    private constructor(noFim: No, distancia: number) {
        this.feromonioAtual = 1;
        this.noFim = noFim;
        this.distancia = distancia;
    }
}

You create objects using the factory method, not the constructor:

const aresta1 = Aresta.GetInstance(null, 0);

const aresta2 = Aresta.GetInstance(null, 10);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.