5

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Bsa0
  • 2,631
  • 5
  • 17
  • 23

2 Answers2

4

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.

lilezek
  • 6,976
  • 1
  • 27
  • 45
  • The node inside the object `mutable` will not be undefined, but `this._root` will continue to be undefined. – Bsa0 Jul 16 '17 at 18:56
  • 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. – lilezek Jul 16 '17 at 18:59
  • 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. – Peter Duniho Jul 16 '17 at 19:59
-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.

Bruford
  • 471
  • 1
  • 3
  • 13
  • 1
    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 Jul 16 '17 at 19:29
  • @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. – Bruford Jul 16 '17 at 19:51
  • _" 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. https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_reference and https://stackoverflow.com/q/373419. – Peter Duniho Jul 16 '17 at 19:57
  • And the method's return value is wrong. It should be `Node`, not `void`. – lilezek Jul 16 '17 at 20:07
  • Sure...removed the offending words. The solution itself remains unchanged edit: oops forgot to update return type – Bruford Jul 16 '17 at 20:10