Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Default template arguments #42

Open
1 task
widberg opened this issue Mar 28, 2023 · 2 comments
Open
1 task

[Feature] Default template arguments #42

widberg opened this issue Mar 28, 2023 · 2 comments

Comments

@widberg
Copy link

widberg commented Mar 28, 2023

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

@WerWolv
Copy link
Owner

WerWolv commented Mar 28, 2023

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>;

@BobSmun
Copy link
Contributor

BobSmun commented Aug 26, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants