wurstscript / WurstScript

Programming language and toolkit to create Warcraft III Maps
https://wurstlang.org
Apache License 2.0
225 stars 30 forks source link

Generics aren't applied across a function returning a generic closure #780

Closed theQuazz closed 5 years ago

theQuazz commented 5 years ago

Here is a demonstration:

https://bin.wurstlang.org/viqegokeju.wurst

function take<T>(int numElems, LinkedList<T> list) returns LinkedList<T>
    return list.take(numElems)

interface TakeNumElems<T>
    function call(LinkedList<T> list) returns LinkedList<T>

function take<T>(int numElems) returns TakeNumElems<T>
    return list -> take(numElems, list)

@Test
function testTake2()
    let ints = asList(1, 2, 3)
    let take2 = take(2)
    take2.call(ints)

Error:

Wrong argument for parameter list: expected LinkedList<T>, but found LinkedList<integer>

image

We can fix it by applying the type to the function like this: let take2 = take<int>(2)

function take<T>(int numElems, LinkedList<T> list) returns LinkedList<T>
    return list.take(numElems)

interface TakeNumElems<T>
    function call(LinkedList<T> list) returns LinkedList<T>

function take<T>(int numElems) returns TakeNumElems<T>
    return list -> take(numElems, list)

@Test
function testTake2()
    let ints = asList(1, 2, 3)
    let take2 = take<int>(2)
    take2.call(ints)
peq commented 5 years ago

Thanks for the report. This problem is already being tracked in #775, so I will close this.

Frotty commented 5 years ago

@peq But shouldn't this work with inference? Or does it need the generic param? Also related to new generics? Or would this never work in the first place.

peq commented 5 years ago

Wurst currently does not use the return type for inferring type arguments and in this case it's not even clear what the return type must be without looking at the following lines.