-
Notifications
You must be signed in to change notification settings - Fork 260
Discussion: Optional declarations and syntax sugar #27
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
I like the idea of The thing is, I'm not sure if it's achievable without breaking the goal of context-free grammar. My additional concern is how to make those extensions work when the user cannot use the compiler-provided stdlib. I work at a kernel driver AG work, so instead of stdlib with its global allocators and system calls, we use our own implementation. Embedded folks will bd in similar situation. How to let Cpp2 know that |
Regarding the null propagation operator if (!a) return nullptr;
auto b = a->getB();
if (!b) return nullptr;
auto c = b->getC();
if (!c) return nullptr;
return c->foo();
// becomes
return a?->getB()?->getC()?->foo(); The issue I always see cited is that while flattening nulls is an obvious thing to do for pointers (or references in other languages), it is not so clear cut for optional, as C++ treats int Object::foo();
obj?->foo(); // returns optional<int>
optional<int> Object::bar();
obj->?bar(); // returns optional<int> or optional<optional<int>>?
Object Object::parent();
optional<Object> Object::next();
obj?->next()?->parent()?->bar(); // returns what? |
I would think this isn't a problem this layer should be concerned with, but a layer below it. You also might have to deal with third-party C++ code that uses |
I am not sure why Herb choose Context-Free grammar, because everything less or more interesting always contains Not Context-Free grammar. Also having C++ naming style for containers with |
The answer lies in the CppCon 2022 link at https://github.com/hsutter/cppfront#wheres-the-documentation. |
Do we have data that this is what we encourage C++ developers to do today by hand? I'm a fan of making default the things we already teach. See this 1-min clip from the talk. I'm especially wary of the C# treatment of optional and null, because for years now I've been hearing from the C# design team that plumbing nullability through the C# language cost more than it was worth. We should learn from the experience of other languages, not only what they did but how it worked out for them and whether they would do it again. (I don't know if that experience about language support for nullability was the same as their language support for optionality.) |
I'm not sure what "we teach" C++ developers about
Well, I get that back-porting a feature into a well-established language can be a potential nightmare, but Cpp2 isn't really "there" yet, right? Also, considering you're just doing code generation at this point, and not writing a Cpp2 compiler, I'd imagine the cost of doing experiments like these are also pretty low?
Right… and why I brought up Swift-- to me it does optionals right, and to your point I think they'd definitely do it the same way again. C# was mentioned for the sake of those reading that were not familiar with Swift. I did watch the entire talk, and I do believe I understand your motivation being something like "to have a language that enforces the things we should be doing with C++." I'm excited to see where you go with it. (Particularly with classes, generics / templates, const correctness, and maybe--fingers crossed-- optionals.) When I saw the |
There might be some overlap with the work for "throwing values". |
Maybe @lattner would like to confirm or deny. He's usually responsive. |
I'm not sure what the specific question is, but yes, I'm pretty happy with the design of optionals in swift. The one thing I'd do different is axe |
Can you elaborate (even if only passing on second-hand knowledge) on what the (unexpected?) costs were? |
As I understood it, it was mainly because it ended up being viral throughout the language specification... many language features ended up with special logic to account for nulls. |
So based on what I understand about cppfront and Cpp2 as it stands, there's a
union
keyword that effectively infers / uses anstd::variant
wherever it's used, and it's invisible to the end-user-- I think that's great and a good choice.However, it looks like when using optionals, you still have to declare them with the original C++ syntax… I think there's a missed opportunity here that has been well treaded in C# (via "Nullables"), JavaScript / TypeScript, and Swift.
If you look at Swift's Optionals, we have declaration like this:
And accessing values are like this (the last two lines are called "optional chaining" in most languages):
Swift also provides the concept of "unwrapping" an optional; I don't want to muck up the above with this, but it would be negligent of me not to mention it:
In my opinion, Swift is a very "Optional first" type of language, meaning you often want to see if something "can be" an optional before you even consider a non-optional type, and I think this kind of thinking might also benefit C++… and maybe a good vector to do this would be through Cpp2 / cppfront.
If for nothing else the syntax sugar of using a declaration of
type?
to implystd::optional<type>
and providing support for optional chaining (thec?.bark()
instead of writingif (*c) { c->bark(); }
would be tremendous in my opinion.One other thing to consider about Optionals in general (as well as how they currently stand in Cpp2), is that the absence of a value is typically represented as
null
ornil
or (in the case of C++)nullopt
. I do agree (and totally support) the idea of eliminatingNULL
in Cpp2, but I think they track you'll find here is similar to Swift, wherenil
(their version ofnull
) only exists to support the "absense of a value in an optional."Whether or not you wanted to go down a similar "unwrap" path would be up to you (and dependent on if you even wanted to go down this "syntax sugar" idea or not), but I think there's lots to learn from other languages that do optionals that C++ can't do without considerable effort that are worth considering here.
tl;dr:
union
->std::variant
b: Int?
c?.bark()
b ?? 5
nullopt_t
is okay, but maybe there's a better long-term naming solution)The text was updated successfully, but these errors were encountered: