[enhancement] Accessing types from impl
- Dominant language
- C++
- Stars
- 1.9k
- Forks
- 283
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 135
Description
### What's hard to do? (limit 100 words)
Right now, it is possible to define constants and types within implementations.
However, outside of the implementation, only the constants are accessible, not the types. They are often
useful for downstream use. Consider the following code. Note the commented out line attempting to access the type inside `type_access_test()`
```Rust
import std;
pub struct Foo {
// type ValueWidth = uN[std::clog2(SIZE)]; // would even better here.
x: uN[std::clog2(SIZE)],
}
impl Foo {
const MY_SIZE = SIZE;
type ValueWidth = uN[std::clog2(SIZE)];
}
#[test]
fn type_access_test() {
type MyFoo = Foo<42>;
const MY_FOO_SIZE = MyFoo::MY_SIZE; // access of constants: works
//type MyValueWidth = MyFoo::ValueWidth; // <--- access of types: does not.
assert_eq(MY_FOO_SIZE, 42);
}
```
As an _extra bonus_ (somewhat unrelated to this problem here), it would actually be useful to declare types directly within a struct (see the 'would even better here') part. That then would allow to improve readability of member declarations, e.g. the `x` declaration could be an `x: ValueWidth`.
### Current best alternative workaround (limit 100 words)
currently there is no way to access the type, so workaround is to copy the type declaration manually in places where they are needed, which is error prone.
### Your view of the "best case XLS enhancement" (limit 100 words)
Best case:
* type defined in `impl` can be accessed internally.
* Even bester: `type` (and, why not `const`) should be possible to declare directly in the struct. They are fields that don't use any space, but can be useful already while inside the struct definition of members. Similar to `const` in impl, they should be accessible as static member `Foo<32>::Type_OR_CONSTANT`
* ^ at that point might be the question if we should only allow `type` and `const` inside the struct and not the `impl` to avoid confusion.
Contributor guide
Research direction
Start by reproducing the example in the XLS language and locate the compiler tests or implementation handling types and impl members. The core completion criterion is that a type declared in an impl can be accessed through an instantiated type; struct-level type and const declarations are additional, unresolved scope.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100