Instantiating an object from a subtype stored in an array

Consider the following example code.

using { /Verse.org/Random }

base_class := class<unique><concrete>:
    Name:string = ""

subclass_a := class(base_class):
    ValueA:int = 123

subclass_b := class(base_class):
    ValueB:float = 456.0

subclass_c := class(base_class):
    ValueC:string = "789"

UsageExample():void =
    ClassType := base_class
    Instance := ClassType: # type(base_class, base_class)
        Name := "Wobbles"

    Print("Created an object named '{Instance.Name}'")

    SubclassType := subclass_a
    SubclassInstance := SubclassType: # type(subclass_a, subclass_a)
        Name := "Chuckles"
        ValueA := 99

    Print("Created an object named '{SubclassInstance.Name}' with value '{SubclassInstance.ValueA}'")

    AllSubclassTypes := array:
        subclass_a
        subclass_b
        subclass_c

    RandomIndex := GetRandomInt(0, AllSubclassTypes.Length - 1)
    if (SelectedType := AllSubclassTypes[RandomIndex]):
        SelectedType{} # FAILS with "false is not a macro.(3545)"

The compiler fails at the commented line stating “false is not a macro”

It seems like the array is of type []subtype(base_class) and hence SelectedType is of type subtype(base_class)

This differs from the types noted in the previous tests and seems to be the cause of the problem. I suspect that this is currently unsupported, but thought I would ask just in case I’m missing something.

Does anyone know how to wrangle the selection back into a type that can be instantiated or which type should be used to store the subtypes in the first place?

A work around for this is to use an explicit type in the array declaration:

    AllSubclassTypes:[]concrete_subtype(base_class) := array:
        subclass_a
        subclass_b
        subclass_c

Looks like you’re on the right track — subtype(base_class) can’t be directly instantiated with {} like a regular class type. One workaround is to define a factory method on each subclass and store function references instead of raw types. This way, you can call the function to create an instance without hitting the macro issue. Definitely seems like a current limitation in how dynamic subclass handling works in Verse.