-
Notifications
You must be signed in to change notification settings - Fork 260
[BUG] t: @struct <T> type
doesn't produce a templated class
#695
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
Comments
This used to error, saying " This is intended, as it's part of the grammar (as evidenced by |
Is there a reason that the syntax isn't like this? MyStruct: <T> @struct type = {} |
It might give the false sense that you could pass the template parameters to the metafunction. From commit d8c1a50:
|
Thanks! Yes, this is max munch, but it should get a diagnostic. With the next commit,
The following code already worked, but now the error guides you to it:
Again, thanks. |
Thanks @JohelEGP. Also from commit 881f86f:
This example is similar (but not exactly) to your suggestion "Down with
I think |
What if a meta function has a template argument and I suddenly forget about max munch? T: type = { ... }
X: @meta <T> type = { ... } It compiles fine, but T: type = { ... }
X: @meta <T: type> type = { ... } Now, if the programmer write this code, they would be surprised why it doesn't work: let x: X<i32> = ...; Novice programmers (like me) wouldn't like to fight with the complexity of the grammar. |
What if I want to pass generic parameter to a meta function? Color: @enum<T> <T: type> type = {
r; b; g;
}
let c: Color<i32> = ...; Is it possible with the current grammar? If it's like Cpp1, the answer is probably no, because the generic parameter is used before the generic argument declaration. Now compare with this syntax, it would be readable and possible: // Color: <T> @enum<T> type = {
Color: <T: type> @enum<T> type = {
r; b; g;
}
let c: Color<i32> = ...; Maybe it's an out of scope feature, and meta functions won't ever be able to get their generic parameter from a generic argument of a declaration. |
@hsutter, would Cpp2 possibly allow function declarations with meta functions in the future? If the answer is yes, what if the meta function is placed before X: <T, U> (a: T, b: U) @meta<decltype(a[b])> = {...}
A: <T> type @meta<T> = {...} It allows the programmer to work with function arguments and pass them to the meta function. |
I was wrong. You can't test it with
template<typename T> class machine {
private: T _value; private: constexpr machine(cpp2::in<cpp2::i64> val);
private: constexpr auto operator=(cpp2::in<cpp2::i64> val) -> machine& ;
public: [[nodiscard]] constexpr auto get_raw_value() const& -> T;
public: constexpr machine(machine const& that);
public: constexpr auto operator=(machine const& that) -> machine& ;
public: constexpr machine(machine&& that) noexcept;
public: constexpr auto operator=(machine&& that) noexcept -> machine& ;
public: [[nodiscard]] auto operator<=>(machine const& that) const& -> std::strong_ordering = default;
public: static const machine off;
public: static const machine on;
public: [[nodiscard]] auto to_string() const& -> std::string;
public: friend auto operator<<(std::ostream& o, cpp2::in<machine> val) -> std::ostream&;
}; |
Thanks for the feedback, I'll think again about the grammar position of applied metatypes. But for the original question, I was actually thinking of a different fix... just like for postfix-expressions any postfix operators must not have preceding whitespace between the identifier and the operator, I think for id-expressions any postfix qualifiers (i.e., the template argument list) must also not have preceding whitespace. I think that's actually clear for programmers, and most of the time they wouldn't even notice the rule because you just naturally write |
OK, I've pushed the commit: fa9ba7f Now the original code just works and does the right and expected thing:
Thanks again! |
This is a better solution for hsutter#695 Now `S: @struct <T> type` just works and does the expected thing
Compiling the following Cpp2 code
produces a non-templated
class
.Explicitly stating that
T
is atype
parameter resolves this issue, but I wonder if this is the intended behavior.compiles to
This behavior is in contrast to custom types without the
@struct
metafunction.compiles to
The text was updated successfully, but these errors were encountered: