[Feature] Default template arguments
What feature would you like to see?
Overrideable default values for template arguments like in C++.
struct Array<T = u32, auto Size = 10> {
T data[Size];
};
How will this feature be useful to you and others?
I have a large number of "optional" structs in my patterns
struct Optional<T, U> {
U isSome;
if (isSome != 0) {
T data;
}
};
99% of the time U is a u8, but sometimes it is a u32. It would be nice to only have to specify U in the rare case rather than every time.
struct Optional<T, U = u8> {
U isSome;
if (isSome != 0) {
T data;
}
};
Optional<SomeOtherStruct> common_optional @ 0x00;
Optional<SomeOtherStruct, u32> uncommon_optional @ 0x10;
Request Type
- [ ] I can provide a PoC for this feature or am willing to work on it myself and submit a PR
Additional context?
No response
Hi
This is absolutely a goal to implement in the future. For now, what I always do is make an alias for it.
Something like using OptionalU8<T> = Optional<T, u8>;
Something to be aware of: Doing the using (at least with values), has the quirk of keeping the original parameters
import std.io;
struct Test<auto t, auto u> {};
using Test1<auto t> = Test<t, t + 1>;
using Test2<auto t> = Test<t + 1, t + 1>;
Test1<5> test1;
Test2<5> test2;
std::print("{} {}", test1, test2);
results in
I: struct Test1 { t = 5, u = 6, t = 5 } struct Test2 { t = 6, u = 6, t = 5 }
Note that t is now defined twice within the same struct. Both are counting toward the pattern limit, and have potentially different values.