Naming conflict between struct parameters and trait alias

Hi, this is not a critical feature, but I think it will enhance code quality when working with traits.

I often find myself using the pattern below and have seen it in files like modular/max/kernels/src/kv_cache/types.mojo

trait MyTrait:
    alias size: Int

struct MyStruct[size_: Int](MyTrait):
    alias size = size_

It would be nicer if either of these were possible:

struct MyStruct[size: Int](MyTrait):
    ... # alias size is automatically defined as size

struct MyStruct[T: AnyType](MyTrait):
    alias size=size # more explicit

Currently, an alias and a parameter cannot use the same name. This results in either:

  • Using two different names for the same thing, which makes the code less readable.
  • Using Self.size (the alias) instead of size_ (the parameter) in the body of MyStruct, which is verbose and inconsistent with how we usually access parameters.

Is this something that it’s worth opening a ticket about?

I agree, we really should fix this. I just ran into this today when introducing an iterator trait.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.