You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[SUGGESTION] const variables with deduced types #190
I can try this one - this looks like a perfect issue for a beginner. I already had a look at it in code version from a couple of days back. @hsutter I saw you cleaned up the treatment of qualifiers. Those update should make fixing this much easier.
Good point, thanks for the bug report. I intended that you have to write the type too if you say anything about the type. Eventually I might allow a wildcard there (e.g., cx : const _ = 42;, that isn't supported today).
Good point, thanks for the bug report. I intended that you have to write the type too if you say anything about the type. Eventually I might allow a wildcard there (e.g., cx : const _ = 42;, that isn't supported today).
My understanding is that cx := 42 is equivalent to cx : auto = 42 and thus auto cx = 42; so it would make sense that cx : const = 42 would be equivalent to cx : const auto = 42 and thus const auto cx = 42;
Since you've said that there is not going to be a volatile qualifier, but rather a type, are there any other type modifiers that could appear in a variable declaration? If there are others, then it may make sense to require a "wildcard type", but if that's the only one, then it's likely clear enough.
OK, this has pushed me to finish adding _ wildcards for local variables and is expressions, as they already work in parameter types and inspect... done!
This test case is now checked in... line 1's and 2's parameters value and x always worked, but the other uses of _ didn't work before, and now newly do work:
less_than: (value:_) -> _ // now works, but see note below
= :(x:_) = x < value$;
main: () -> int
= {
x: const _ = 2; // now works
p: * _ = x&; // now works
q: * const _ = p&; // now works
if x is (less_than(20)) {
std::cout << "yes, less\n";
}
if x is _ { // now works
std::cout << "yes, always\n";
}
}
Note that the less_than example has a deduced return type, which as discussed elsewhere is not order-independent. My aim is to allow non-local deduction in just those cases where it doesn't violate order-independence, such as when deducing from literals in the other thread where this was discussed, but the implementation is currently in a 'partway' state that's successively approximating toward that goal and doesn't currently reject all the code that won't be order-independent. It will need a future diagnostic to reject those cases, or we could decide that it's okay to teach "a function with a deduced return type has to be defined before it's called" as a specific exception to strict order independence. The deduced return type is admittedly useful for lambda generator functions.
When you have some time, if you could explain a little why the _ is important there, it would be appreciated. I'm not seeing the importance, so having some other perspectives would be useful. No rush. (If you've already done that somewhere else, and I've just missed it or forgotten, a reference would be fine.)
My feeling was that it's fine to just write nothing between : and = to deduce the type, but it felt odd to allow saying const and nothing more... maybe I just need to get used to it! I'll keep it in mind.
I guess that question would also apply to these, unless they're also optional and you're just showing how they can be provided if wanted: (value:_) -> _ = :(x:_). I think it makes sense that it's needed in x is _ {.
Maybe unrelated question, in x is (less_than(20)), are the outer () required to indicate an expression and not a type, or can this be x is less_than(20)?
@hsutter When I saw your first answer in this thread I though that as much as I like the idea of having _ as a wildcard I think it might be problematic for Cpp1 compatibility reasons. It is unlikely to be seen in actual code but the following is valid Cpp1:
struct _ {};
Is this not an issue? Of course the problem lies not in the idea of a wildcard but in the choice of the character - it should not be something usable in identifiers. In fact I have been using the fact that _ is a valid identifier in Cpp1 to use it as a "wildcard" for unused names in structured bindings. A proper wildcard would be so much nicer also there.
Maybe ? would do? The choice is yours of course, I could try to make the change that should be on even simpler change 😁.
@jarzec Nope, that shouldn't be an issue because Cpp2 sits in front of the existing language and is in control even of words that are popular in existing code (like potentially _) or reserved in Cpp1 (like new). Cppfront just translates them to something else (e.g., cpp2_new) when lowering to Cpp1, and that lets Cpp2 do things like treat new as an ordinary overloadable member function template because the translated name really is a function.
I just tried this example and it works fine:
// Here's some existing Cpp1 code that provides the name "_"
// (not advisable at global scope like this, but legal and fine)
struct _ {
struct Nested { };
};
// Cpp2 can still use it fine, with qualification
main: ()->int = {
y: _::Nested = ();
}
In cppfront I've tried to translate _ to auto only in the cases where Cpp2 treats it as a wildcard. If I missed any or was too aggressive just post a bug report and I'll take a look. But normally the idea is that you can still use a Cpp1 name that conflicts with a Cpp2 special word legal in the same position (such as a wildcard) could exist just by qualifying the Cpp1 name.
(I just pushed a commit that makes using ::_ work in the above example. It only didn't work because there was a bug where I wasn't recognizing any ::name as a qualified-id, that's now fixed.)
I was more thinking of the particular use in inspect as in this modified bit of mixed-inspect-templates.cpp2:
struct _ {};
// Can I take the _ struct as argument?
fun: (v : _) -> std::string = {
return inspect v -> std::string {
is std::vector = "std::vector";
is std::array = "std::array";
is std::variant = "std::variant";
is my_type = "my_type";
// Can I handle the _ struct?
is _ = "unknown";
};
}
I suspect the answer is "no, you can't", and I'm okay with that answer. I'm okay with losing the ability to name a type or variable as just _ in CPP2 in order to gain the ability to have a "wildcard type" or "discarded variable" capability as in C#, Typescript, Go (and the internet says also Python, Dart, and Haskell).
To be clear, this is just about "in CPP2 code", which already is different than existing code. It should still have the same meaning as before in CPP1.
I suspect the answer is "no, you can't", and I'm okay with that answer.
I would be okay with it too, but I like that the answer is "yes, you can"... Being able to demonstrate compatibility with even these kinds of crazy-ish names is an important part of being able to 100% seamlessly consume existing C++ code, which is the primary "special sauce" goal that sets this project apart from all others, past and present.
Reacted by jarzec, Filip Sajdak and Ezekiel Warren
Activity
jarzec commentedon Jan 3, 2023
I can try this one - this looks like a perfect issue for a beginner. I already had a look at it in code version from a couple of days back.
@hsutter I saw you cleaned up the treatment of qualifiers. Those update should make fixing this much easier.
hsutter commentedon Jan 4, 2023
Good point, thanks for the bug report. I intended that you have to write the type too if you say anything about the type. Eventually I might allow a wildcard there (e.g.,
cx : const _ = 42;, that isn't supported today).I'm now adding a diagnostic... thanks!
Update: @jarzec sorry for missing your note!
gregmarr commentedon Jan 4, 2023
My understanding is that
cx := 42is equivalent tocx : auto = 42and thusauto cx = 42;so it would make sense thatcx : const = 42would be equivalent tocx : const auto = 42and thusconst auto cx = 42;Since you've said that there is not going to be a volatile qualifier, but rather a type, are there any other type modifiers that could appear in a variable declaration? If there are others, then it may make sense to require a "wildcard type", but if that's the only one, then it's likely clear enough.
Add support for `_` wildcard in variable decls and `is` expressions (…
hsutter commentedon Jan 4, 2023
OK, this has pushed me to finish adding
_wildcards for local variables andisexpressions, as they already work in parameter types andinspect... done!This test case is now checked in... line 1's and 2's parameters
valueandxalways worked, but the other uses of_didn't work before, and now newly do work:Note that the
less_thanexample has a deduced return type, which as discussed elsewhere is not order-independent. My aim is to allow non-local deduction in just those cases where it doesn't violate order-independence, such as when deducing from literals in the other thread where this was discussed, but the implementation is currently in a 'partway' state that's successively approximating toward that goal and doesn't currently reject all the code that won't be order-independent. It will need a future diagnostic to reject those cases, or we could decide that it's okay to teach "a function with a deduced return type has to be defined before it's called" as a specific exception to strict order independence. The deduced return type is admittedly useful for lambda generator functions.gregmarr commentedon Jan 4, 2023
Looks good.
When you have some time, if you could explain a little why the
_is important there, it would be appreciated. I'm not seeing the importance, so having some other perspectives would be useful. No rush. (If you've already done that somewhere else, and I've just missed it or forgotten, a reference would be fine.)hsutter commentedon Jan 4, 2023
You mean for the variable declaration cases...
My feeling was that it's fine to just write nothing between
:and=to deduce the type, but it felt odd to allow sayingconstand nothing more... maybe I just need to get used to it! I'll keep it in mind.gregmarr commentedon Jan 4, 2023
I guess that question would also apply to these, unless they're also optional and you're just showing how they can be provided if wanted:
(value:_) -> _ = :(x:_). I think it makes sense that it's needed inx is _ {.Maybe unrelated question, in
x is (less_than(20)), are the outer()required to indicate an expression and not a type, or can this bex is less_than(20)?hsutter commentedon Jan 4, 2023
Yes, those parens are currently required around
is (predicate).gregmarr commentedon Jan 4, 2023
Thanks.
jarzec commentedon Jan 4, 2023
@hsutter When I saw your first answer in this thread I though that as much as I like the idea of having
_as a wildcard I think it might be problematic for Cpp1 compatibility reasons. It is unlikely to be seen in actual code but the following is valid Cpp1:Is this not an issue? Of course the problem lies not in the idea of a wildcard but in the choice of the character - it should not be something usable in identifiers. In fact I have been using the fact that
_is a valid identifier in Cpp1 to use it as a "wildcard" for unused names in structured bindings. A proper wildcard would be so much nicer also there.Maybe
?would do? The choice is yours of course, I could try to make the change that should be on even simpler change 😁.hsutter commentedon Jan 5, 2023
@jarzec Nope, that shouldn't be an issue because Cpp2 sits in front of the existing language and is in control even of words that are popular in existing code (like potentially
_) or reserved in Cpp1 (likenew). Cppfront just translates them to something else (e.g.,cpp2_new) when lowering to Cpp1, and that lets Cpp2 do things like treatnewas an ordinary overloadable member function template because the translated name really is a function.I just tried this example and it works fine:
In cppfront I've tried to translate
_toautoonly in the cases where Cpp2 treats it as a wildcard. If I missed any or was too aggressive just post a bug report and I'll take a look. But normally the idea is that you can still use a Cpp1 name that conflicts with a Cpp2 special word legal in the same position (such as a wildcard) could exist just by qualifying the Cpp1 name.(I just pushed a commit that makes using
::_work in the above example. It only didn't work because there was a bug where I wasn't recognizing any::nameas a qualified-id, that's now fixed.)jarzec commentedon Jan 5, 2023
I was more thinking of the particular use in
inspectas in this modified bit ofmixed-inspect-templates.cpp2:gregmarr commentedon Jan 5, 2023
I suspect the answer is "no, you can't", and I'm okay with that answer. I'm okay with losing the ability to name a type or variable as just
_in CPP2 in order to gain the ability to have a "wildcard type" or "discarded variable" capability as in C#, Typescript, Go (and the internet says also Python, Dart, and Haskell).To be clear, this is just about "in CPP2 code", which already is different than existing code. It should still have the same meaning as before in CPP1.
hsutter commentedon Jan 5, 2023
@jarzec:
Again, great question. Yes, just qualify the name... this program works:
@gregmarr:
I would be okay with it too, but I like that the answer is "yes, you can"... Being able to demonstrate compatibility with even these kinds of crazy-ish names is an important part of being able to 100% seamlessly consume existing C++ code, which is the primary "special sauce" goal that sets this project apart from all others, past and present.
gregmarr commentedon Jan 6, 2023
Oooh, even better. I hadn't considered consuming a CPP1 type in a CPP2 function, so it's good that it works.
Require a type name if the user starts to specify type qualifiers, cl…
Add support for `_` wildcard in variable decls and `is` expressions (…
Require a type name if the user starts to specify type qualifiers, cl…
Allow `x: const = init;` and `x: * = init;` without `_` type wildcard
Allow `x: const = init;` and `x: * = init;` without `_` type wildcard