shader-slang / slang

Making it easier to work with shaders
MIT License
2.02k stars 172 forks source link

Enable lookup of extension members on builtin numeric types #2092

Closed jsmall-zzz closed 9 months ago

jsmall-zzz commented 2 years ago

Take the following example

struct Thing
{
    int v;
};

interface IOp
{
    This doOp();
};

extension Thing : IOp
{
    This doOp()
    {
        Thing b = this;
        b.v++;
        return b;
    }
};

extension int : IOp
{
    This doOp()
    {
        // Might be confusing to C++ programmers. 
        // as 'this' isn't a pointer but the int *value*
        return this + 1;
    }
}

Doing

int a = ...;
a.doOp();

Produces multiple error messages of the form

.slang(42): error 30052: invalid swizzle pattern 'doOp' on type 'int'

Whereas

Thing thing = ...;
thing.doOp();

Works as expected.

Note a workaround that may be usable to sidestep this issue is to use a generic. For example

T doOp(T : IOp v) { return v.doOp(); }

// Works
doOp(a);
natduca commented 9 months ago

Bankrupcy, can re-create when customer use case appears.