Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have instances of a generic function #3057

Closed
f3c0 opened this issue May 6, 2015 · 4 comments
Closed

Have instances of a generic function #3057

f3c0 opened this issue May 6, 2015 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@f3c0
Copy link

f3c0 commented May 6, 2015

Can I have an instance of a generic function?

For example I have a function like this:

function pushType<T>(item: T, list: T[]): T[]{
    list.push(item);
    return list;
}

If I would like to use with number parameter in multiple places, I have to do this:

pushType<number>(12, [3, 4, 5]);
pushType<number>(1, [4, 5]);

Can I have an instance of this function for numbers instead of?
Something like this:

var pushNumber = pushType<number>; // this is syntax error now
pushNumber(12, [3, 4, 5]);
pushNumber(1, [4, 5]);

var pushNumber = pushType<number>; is now synax error and compiles to var pushNumber = pushType();
It would be nice I guess to compile it to var pushNumber = pushType;

@CyrusNajmabadi
Copy link
Contributor

You can just do:

var pushNumber: (i: number, list: number[]) => number[] = pushType;

@danquirk
Copy link
Member

danquirk commented May 7, 2015

Note there's no need to explicitly specify the generic type argument here.

function pushType<T>(item: T, list: T[]): T[]{
    list.push(item);
    return list;
}

pushType(12, [3, 4, 5]); // ok
pushType(1, [4, 5]); // ok
pushType(1, ['']); // error

@danquirk danquirk added the Question An issue which isn't directly actionable in code label May 7, 2015
@f3c0
Copy link
Author

f3c0 commented May 7, 2015

Thanks danquirk. No need to explicit specify in that case, I agree.
However if I have the var pushNumber = pushType<number>; than a pushNumber('1', ['2']); is an error while the pushType('1', ['2']); is fine, so it's like a restricted version of a generic function.

CyrusNajmabadi's comment looks a solution for now, thanks. But I feel my my suggesion more elegant. Do you think is it against any language design or can it be a valid suggestion to implement later?

@danquirk
Copy link
Member

danquirk commented May 8, 2015

You might also be interested in #1616

The problem right now is that the system is not designed in such a way that you can instantiate a generic with a particular type and then 'finish' using it later (ie provide arguments). Certainly people are accustomed to doing something like what you are proposing in certain other languages with different implementations of generics. Given that there are a couple good workarounds (Cyrus' as well as defining a separate function constrained specifically to number which then forwards the call to a more generic version) we would likely need some more compelling use cases to consider complicating the type system like this.

@mhegazy mhegazy closed this as completed May 18, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants