dslx should support struct level constant
DSLX currently supports top-level, proc and fn constants, it would be nice to have struct level constant as well to allow users to define bit-width constants related to a type without the need to prefix them.
This would come handy in module like https://github.com/google/xls/blob/main/xls/dslx/stdlib/apfloat.x:
pub struct APFloat<EXP_SZ:u32, FRACTION_SZ:u32> {
sign: bits[1], // Sign bit.
bexp: bits[EXP_SZ], // Biased exponent.
fraction: bits[FRACTION_SZ], // Fractional part (no hidden bit).
const EXP_SZ = EXP_SZ,
const FRACTION_SZ = FRACTION_SZ,
}
where type parameter like EXP_SZ and FRACTION_SZ could be then exposed on aliased type instances:
import apfloat;
type F32 = apfloat::APFloat<F32_EXP_SZ, F32_FRACTION_SZ>;
fn qnan() -> F32 { apfloat::qnan<F32::EXP_SZ, F32::FRACTION_SZ>() }
I think it would be good if we can access the provided template parameters directly as const value (like we can access the template parameters for a function in the scope of that function as const value).
So:
pub struct APFloat<EXP_SZ:u32, FRACTION_SZ:u32> {
// regular fields, no extra constants
};
pub type F32 = apfloat::APFloat<8, 23>;
We could access F32::EXP_SZ without additional need to provide consts in the struct as that template-implied constant is already there. Less typing, less opportunities for mistakes.
(but having also constants in that struct wouldn't harm, e.g. if we want to have some derived values such as
const MANTISSA_SIZE = u32:1 + FRACTION_SZ;
)