Accessing associated constants from variable

Hey :slight_smile:

Is it possible to access an associated constant of a trait implementation without explicitly naming the type?

Playground link

My goal is to access C without using S, only s

Not that I’m aware, but if you’re inside a generic function, you can use the type parameter name:

fn f<A:T>(x:A)->usize {
    <A as T>::C    // A::C might also work; not sure

Thanks :slight_smile:
I've thought about it, but I need to use C in a const context to define another type, and using a function won't really work in that context. (i.e. you couldn't assign the result of this function to a const, even if the function was marked const)

The function isn’t important, it’s just one way to get a generic name for a type. You can always do <Some<Complicated,Type>::Expression as T>::C. I don’t think there’s currently any support for any unnameable types in constexprs so you should be able to specify it somehow (though it may be extremely cumbersome).

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.