Uh oh!
There was an error while loading. Please reload this page.
Type parameters as constraints - #5949
Conversation
Conflicts: tests/baselines/reference/functionConstraintSatisfaction2.errors.txt
There was a problem hiding this comment.
Invert the condition to reduce nesting.
There was a problem hiding this comment.
Not sure what you mean. There's a shared return statement at the bottom of the function and no else clause that would be reversed.
There was a problem hiding this comment.
I meant to just repeat the return statement, but it's not really a big deal.
Anders Hejlsberg (ahejlsberg)
commented
Dec 8, 2015
Mohamed Hegazy (@mhegazy) Want to comment on this one before I merge it? |
Mohamed Hegazy (mhegazy)
commented
Dec 9, 2015
👍 |
Anders Hejlsberg (@ahejlsberg) I tried this out and upon grabbing quick info on interfaceMapper<T>{map<UextendsT,VextendsU[]>(f: (item: T)=>U): V;}varm: Mapper<string>;vara=m.map((x: string)=>x);I expected |
Anders Hejlsberg (ahejlsberg)
commented
Dec 10, 2015
Daniel Rosenwasser (@DanielRosenwasser) Good catch. The problem is that the constraints aren't getting properly instantiated. Will put up a fix shortly. |
Hey Anders Hejlsberg (@ahejlsberg), Aleksey-Bykov wrote a useful example on #6037 which demonstrates the use of type parameters being able to extend each other. Can we add the following as a test case? functionfold<a,r>(values: a[],result: r,fold: (result: r,value: a)=>r): r{for(letvalueofvalues){result=fold(result,value);}returnresult;}functionappend<a,bextendsa>(values: a[],value: b): a[]{values.push(value);returnvalues;}fold([1,2,3],[]as[string,string][],(result,value)=>append(result,["",""])); |
ZpdDG4gta (zpdDG4gta8XKpMCd)
commented
Dec 10, 2015
by the way // test.tsexportfunctionappend<a,bextendsa>(result: a[],value: b): a[]{result.push(value);returnresult;}compiles with but fails with saying |
ZpdDG4gta (zpdDG4gta8XKpMCd)
commented
Dec 10, 2015
created a separate bug: #6040 |
I noticed the same thing on my bug where the type parameter was not being instantiated. Can you enable |
Anders Hejlsberg (ahejlsberg)
commented
Dec 10, 2015
Latest commits fix the uninstantiated type issue as well as issue in #6040. |
Basarat Ali Syed (basarat)
commented
Dec 10, 2015
Awesome 🌹. I've done aweful things in the past : https://github.com/TypeScriptBuilder/tsb/blob/24c5bbaa3e29587ab0830194c374cd7fa8bb2462/src/app/state/simpleRedux.ts#L90-L103 :) |
Anders Hejlsberg (@ahejlsberg) I think it wouldn't be unreasonable to just get declaration emit for all of these tests. |
Type parameters as constraints
ZpdDG4gta (zpdDG4gta8XKpMCd)
commented
Dec 11, 2015
Basarat Ali Syed (@basarat), a poorman's implementation of this feature could have been done by passing foo <a, b extends a>(foo: a, bar:b): void; vs foo <a, b>(foo: a, bar: b, bIsA: (val: b) => a): void; |
Claudia Meadows (dead-claudia)
commented
Dec 17, 2015
Question: does this patch support the following? I've come across this pattern way too frequently, and I've needed it for way too many things. In languages that support it, it's a very frequent idiom. interfaceType<TextendsType<T>>{} |
Anders Hejlsberg (ahejlsberg)
commented
Dec 17, 2015
@isiahmeadows Yes, that pattern (the "curiously recurring template pattern") is now supported. But also note that the polymorphic 'this' type is a more succinct way to accomplish the same thing. |
Claudia Meadows (dead-claudia)
commented
Dec 17, 2015
I also came across an unusual use case where I needed two F-bounded type On Thu, Dec 17, 2015, 09:31 Anders Hejlsberg notifications@github.com
|

With this PR it becomes possible for a type parameter constraint to reference type parameters from the same type parameter list (this was previously an error). For example:
A fancy term for this capability is F-Bounded Polymorphism.
Fixes#2304.