Issue with type inference in Verse generics

Please select what you are reporting on:

Unreal Editor for Fortnite

What Type of Bug are you experiencing?

Verse

Summary

HI all,

I’m trying to write a generic function that finds the indices of duplicate entries in an array. The problem I’m having is with the line Key > MaxElement. The compiler can’t infer that the type of MaxElement and Key are the same:

    Find_Equal_Elements<public>(Array: []t where t:subtype(comparable))<transacts>:[]int=
        if (Array.Length <= 1):
            return array{0}

        var Duplicates: [t][]int = map{}
        
        for (Index->Element : Array):
            if (set Duplicates[Element] += array{Index}) {}
        
        var Indices: []int = array{0}

        if (var MaxElement: t = Array[0]):
            for (Key->Value : Duplicates):
                if (Key > MaxElement):
                    set MaxElement = Key
        
            
            if (ElementIndices := Duplicates[MaxElement]):
                set Indices = ElementIndices
            
        Indices

Steps to Reproduce

Enter the above code in VS Code and you’ll see the compiler error about no overload operator for >

Expected Result

I expect the compiler to infer that both types are the same

Observed Result

Error

Platform(s)

Windows 10

Hi smckdwn16, the comparable interface only provides the operators = and <> (inequality) see; comparable documentation , lesser and greater comparison is only available for int and float which do not share a common interface (although I hope in the future we have a “magnitudable” type or trait for such a purpose)

2 Likes

Hi Noldstein. Thanks for the response.

The issue isn’t that an interface is missing. It’s that the compiler isn’t inferring the type correctly imo.

For instance: var Element: t = Array[0] should be inferred as type int if an int array is passed in. If Element is then compared to other elements in that same array, the types should be inferred correctly.

However, I do see how a magnitudable trait would be beneficial.

Thought about this more and realized you are correct. There needs to be an interface for this similar to the PartialOrd trait in Rust.