Skip to content

Dynamic Dispatch: Dynamic Dispatch of Special Members #9887

@wlewNV

Description

@wlewNV

Description

Special interface members - constructors (__init), subscript operators (__subscript), function call operators (operator()), and property accessors - should all work through dynamic dispatch. This also includes generic versions of these members.

Test Cases

interface IFullFeatured
{
    // Constructor
    __init(int value);
    
    // Property
    property float value { get; set; }
    
    // Subscript operator
    __subscript(int i) -> float { get; set; }
    
    // Function call operator
    float operator()(float x, float y);
    
    // Regular method for comparison
    int getValue();
}

struct ImplA : IFullFeatured
{
    int _val;
    float _data[4];
    
    __init(int value) { _val = value; }
    
    property float value
    {
        get { return float(_val); }
        set { _val = int(newValue); }
    }
    
    __subscript(int i) -> float
    {
        get { return _data[i]; }
        set { _data[i] = newValue; }
    }
    
    float operator()(float x, float y) { return x + y; }
    
    int getValue() { return _val; }
}

struct ImplB : IFullFeatured
{
    int _val;
    float _data[4];
    float _scale;
    
    __init(int value) { _val = value * 2; _scale = 2.0; }
    
    property float value
    {
        get { return float(_val) * _scale; }
        set { _val = int(newValue / _scale); }
    }
    
    __subscript(int i) -> float
    {
        get { return _data[i] * _scale; }
        set { _data[i] = newValue / _scale; }
    }
    
    float operator()(float x, float y) { return x * y; }
    
    int getValue() { return _val; }
}

IFullFeatured makeObj(int id)
{
    if (id == 0) return ImplA(10);
    else return ImplB(10);
}

void test()
{
    IFullFeatured obj = makeObj(0);
    
    // Property access
    obj.value = 5.0;
    float v = obj.value;
    
    // Subscript access
    obj[0] = 3.0;
    float s = obj[0];
    
    // Function call operator
    float r = obj(2.0, 3.0);
    
    // Regular method
    int x = obj.getValue();
}

Expected Behavior

All special members compile and dispatch correctly at runtime:

  • Constructor initializes the correct implementation
  • Property get/set dispatches to correct accessors
  • Subscript get/set dispatches to correct implementation
  • Function call operator dispatches correctly

Notes

  • Also test generic versions: __subscript<T>(T index) -> float
  • Consider autodiff versions of differentiable members
  • May need separate negative tests if any of these should NOT work

Metadata

Metadata

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions