Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out and ref in TypeScript [duplicate]

Tags:

typescript

Is it possible to include output parameters in a function with TypeScript? Something like Func1(string val1, int out k1, int out k2) in C#.

like image 455
user1941679 Avatar asked Jan 01 '13 23:01

user1941679


4 Answers

Not currently.

You can return an object that can contain more than one property.

return { k1: 5, k2: 99 };

You can combine this with destructuring so the intermediate object becomes invisible...

function myFunction() {
    return { k1: 5, k2: 99 };
}

const { k1, k2 } = myFunction();

console.log(k1);
console.log(k2);

You could also achieve the same with a tuple, but this is pretty readable.

like image 60
Fenton Avatar answered Sep 19 '22 07:09

Fenton


Typescript passes all parameters with "call by value". But if the parameter is a reference this behaves similarly to "call by reference" most of the time. You can write wrapper classes for primitive types. Here's some code:

var func=function(param:Str){
    param.str="modified";
}
class Str{
    str:string="unmodified";
}
var test:Str=new Str();
alert(test.str); //alerts "unmodified"
func(test);
alert(test.str); //alerts "modified"

You need to be careful, though:

var func=function(param:Str){
    param=new Str("modified");
}
class Str{
    str:string;
    constructor(param:string){
        this.str=param;
    }
}

var test:Str=new Str("unmodified");
alert(test.str); //alerts "unmodified"
func(test);
alert(test.str); //alerts "unmodified"

The function parameter is passed "call by value". Therefore inside the function body you're working with a copy of a reference. This reference points to the same object as the reference that you passed as a parameter, so you can access its members and modify them. But if you assign a new object to the reference all further changes are applied to this new object. Therefore the code above prints unmodified twice. I think C# works this way, too.

like image 34
lhk Avatar answered Sep 20 '22 07:09

lhk


Generally, you just return an object with multiple properties, one of which contains your function. Something like this:

var foo = function (val1 : string){
    // do something

    return {
        k1: 22,
        k2: 33
    };
}

You could also make it implement an interface, so you know what to expect as the returned object.

interface IFoo {
    (val1: string): INumbers;
}
interface INumbers {
    k1 : number;
    k2 : number;
}

var foo : IFoo = (val1 : string){
    // do something

    return {
        k1: 22,
        k2: 33
    };
}
like image 42
John Papa Avatar answered Sep 20 '22 07:09

John Papa


If you really, really want to have an output parameter, even though you could return an object or an array (as a makeshift tuple object), look at foo and the call site of foo...

function p(s) {
  document.body.appendChild(document.createTextNode(s));
  document.body.appendChild(document.createElement('BR'));
}
function foo(output: any): void {
  output.uno = 1;
  output.dos = 2;
}
var o: any = {};
function foo(o);
p(o.uno + " " + o.dos);
like image 45
Eljay Avatar answered Sep 18 '22 07:09

Eljay