shader-slang / slang

Making it easier to work with shaders
MIT License
1.81k stars 161 forks source link

typealias with interface does not work as expected #4266

Closed fknfilewalker closed 1 month ago

fknfilewalker commented 1 month ago

Static const vars can easily be defined in interfaces and then set in the implementation

interface IFoo {
    static const bool isBar;
};

struct Bar {
    static const bool isBar = true;
};

for typealias this does not work and the error messages are not clear to me e.g., this for instance displays an error in struct Bar: member 'Scalar' does not match interface requirement.(38105) see interface requirement declaration of 'Scalar'

interface IFoo {
    typealias Scalar = void;
};

struct Bar : IFoo {
    typealias Scalar = float;
};

if I don't set anything in Bar I get type 'Bar' does not provide required interface member 'Scalar'(38100) see declaration of 'Scalar'

Even setting Scalar to void in Bar does not work?

Slang version 2024.1.20

csyonghe commented 1 month ago

It is not valid to define a typealias inside an interface. If you wish to define a requirement that all implemnetations need to provide, you are probably looking for associatedtype.

For example:

interface IFoo
{
     associatedtype Scalar : __BuiltinArithmeticType;
}

struct Bar : IFoo
{
    typealias Scalar = float;
}
csyonghe commented 1 month ago

We need to not treat a typealias inside an interface as a requirement for subtypes. Instead it should just mean a type alias in the scope of that interface decl.

csyonghe commented 1 month ago

Or perhaps we should just disallow defining a typealias inside an interface.

fknfilewalker commented 1 month ago

Thank you for the help, this works perfectly

We need to not treat a typealias inside an interface as a requirement for subtypes. Instead it should just mean a type alias in the scope of that interface decl. This would be more in line with the expected behavior I guess