[Verse] Array of subclasses that share an interface is typed []comparable

Summary

When two subclasses share a base class and also implement the same interface, an array literal holding one of each is typed comparable (or any for a non-unique base) instead of the base class. The array can’t be passed where an array of the base class is expected, and declaring the type on a variable doesn’t help.

Please select what you are reporting on:

Unreal Editor for Fortnite

What Type of Bug are you experiencing?

Verse

Steps to Reproduce

  1. Create a blank UEFN project.
  2. Add one Verse file with this code:
has_name := interface:
    Name:string = "unnamed"

animal := class<unique>{}
cat := class(animal, has_name){}
dog := class(animal, has_name){}

PetAll(Animals:[]animal):void = {}

Repro(Cat:cat, Dog:dog):void =
    PetAll(array{ Cat, Dog })
  1. Build Verse code.

Comparison cases (one change each):

  • Remove has_name from dog only: the code compiles.
  • Remove <unique> from animal: the same error, but the array is typed []any instead of []comparable.

Expected Result

The code compiles. cat and dog are both animals, so array{ Cat, Dog } is accepted as []animal.

Observed Result

The build fails with error 3509 on the PetAll(array{ Cat, Dog }) line:

This function parameter expects a value of type []animal, but this argument is an incompatible value of type []comparable.

Declaring the type on a variable first fails the same way:

Animals:[]animal = array{ Cat, Dog }
This variable expects to be initialized with a value of type []animal, but this initializer is an incompatible value of type []comparable.

Platform(s)

Windows PC (UEFN 43.12, ++Fortnite+Release-43.12-CL-60809112; compile error, no session needed)

Additional Notes

Workaround: store the first element in a variable typed as the base class, then build the array from it. This compiles:

First:animal = Cat
PetAll(array{ First, Dog })

Found with Scene Graph components: Entity.AddComponents(array{ MyInventory, MyScreen }) compiled until an interface was added to one component class that the other one already implemented. A change to one class broke an unchanged call elsewhere, and the error message doesn’t mention the interface.

Possible cause (unconfirmed): the compiler can’t choose between animal and has_name as the common type and falls back to comparable (or any). If this is intended, it would help if the declared type of the variable or parameter decided the array’s type, or if the error message named the candidate types.

FORT-1204794 has been ‘Backlogged’. This is an issue that is in a queue to be addressed.