7

In C# it is possible to pass parameters by reference. For instance:

    private void Add(ref Node node)
    {
        if (node == null)
        {
            node = new Node();
        }
    }
    Add(ref this.Root);

this.Root would not be null after doing Add(ref this.Root)

From what I've seen from TypeScript, it is not possible to pass parameters by reference. This code:

    private addAux(node: Node): void {
        if (node === undefined) {
            node = new Node();
        }
    }
    addAux(this._root);

After doing addAux(this._root), this._root will still be undefined because a copy of it will be passed into addAux.

Is there a workaround to have the same feature as the ref keyword from C# in TypeScript?

0

2 Answers 2

6

The sole workaround I can come now with is to do something like this:

private addAux(ref: {node: Node}): void {
    if (ref.node === undefined) {
        ref.node = new Node();
    }
}
let mutable = {node: this._root};
addAux(mutable);

After this, the node inside the object mutable will not be undefined.

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

3 Comments

The node inside the object mutable will not be undefined, but this._root will continue to be undefined.
Yes, this suggestion is about not using this._root anymore, and using mutable.node instead. Otherwise I'm pretty sure you can't mimic the C# ref syntax.
The above would work, but is not IMHO the right way to do it. Instead, work with the language, by returning the new value from the method (so the caller can decide whether to reassign the new value to the variable or not), rather than trying to shoe-horn a fake pass-by-reference into the method.
-2

You could do this?

private addAux(node: Node): Node {
    if (node === undefined) {
        node = new Node();
    }
    return node
}

this._root = addAux(this._root);

As a note - this is a JavaScript question rather than TypeScript. Data types in JS are always pass by value.

5 Comments

Objects are not passed by ref. If that was the case OP wouldn't have to create this question. Reference is passed by value which means that the function gets a copy of it.
@MistyK I'm not sure that whether JS is technically 'pass by ref' or 'pass by object sharing' or 'pass by value but that value is a ref' is actually part of the problem here, I was simplifying for brevity. The issue is that this._root is undefined which is not an object and hence is passed by value - nothing to do with objects.
" I was simplifying for brevity" -- writing "with the exception of objects which are pass by ref" isn't simplifying. It's simply wrong. JavaScript passes everything by value, including the values which are references to objects. Pass-by-reference means that a reference to the variable itself is passed, so that the variable itself may be changed. This is not the same as passing a reference to an object. See e.g. en.wikipedia.org/wiki/Evaluation_strategy#Call_by_reference and stackoverflow.com/q/373419.
And the method's return value is wrong. It should be Node, not void.
Sure...removed the offending words. The solution itself remains unchanged edit: oops forgot to update return type

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.