5 ms·
Swift gets weirder and more difficult, C++ gets better. Will the two lines intersect? Probably not, but I'm starting to wonder. I wish C++ had a few more cosme
by rnikander 4y ago
Swift gets weirder and more difficult, C++ gets better. Will the two lines intersect? Probably not, but I'm starting to wonder.
I wish C++ had a few more cosmetic and convenient things that I've gotten used to in Swift. 1. Properties with get/set/didSet blocks. 2. Non-nullable types, ie `T` and `T?`. There's probably no way to add those, even as a GCC-specific non-standard extension.
- pjmlp 4y agoWell, in spite its warts, I know which ecosystem I would rather invest on.
- gpderetta 4y agoWhat do you mean non nullable types? In C++ only pointers can be nullable.
- darknavi 4y agoAlso with `std::optional` "nullable" pod values are a LOT nicer.
- rnikander 4y agoYears ago I worked on some C++ and the code had a lot of pointers to objects. Maybe it was not well written C++. In Swift (and Kotlin and others) the compiler can prove that you never get a null pointer error at runtime. If you want something that can be null, the type is `T?` (short for `Optional<T>`) instead of `T`. Then there are various syntactically convenient ways of unwrapping it and dealing with the null case. Eg `foo ?? bar` means give me `foo` if it's not null, otherwise `bar`. Or `foo?.x` means: get the property x of foo, if foo is not null. I haven't used C++ much in recent years. If there's an equivalent, I'd love to know about it.
- jcelerier 4y agoC++ objects are values. E.g. in std::string str; str is never null, and always a valid object, because it's a value, not a pointer like in most GC'ed or dynamic languages
- Tyr42 4y agoWait, but what if you have std::string str1{"Apple"}; std::string str2 = std::move(str1); Isn't str1 in an invalid (err, unspecified?) state and you shouldn't use it? It's not `null` sure, but it's not good to use.
- lenkite 4y agoThis issue is caught and flagged by clang-tidy. Though technically the standard says it is in valid but un-specified state. Because of short string optimisation, the move might be possibly a copy, so one should not rely on the contents being empty.
- deschutes 4y agoClang tidy covers only the most trivial use after move scenarios. It's useful coverage but isn't and never will be complete like the rust borrow checker.
- jcelerier 4y agoNo, you can absolutely use it (but you most likely want to clear() or assign an empty string to it before doing e.g. push_back as move does not necessarily clear the moved-from object - std::move(some_int) won't clear the int)
- Tyr42 4y agoI mean, it won't have a useful value, so I need to re assign to it, at which point I could just be using a new variable right? It might not be null, but I can't do much more with it than I can with null. I need to assign something new first
- jcelerier 4y ago> I mean, it won't have a useful value, so I need to re assign to it, at which point I could just be using a new variable right? I have a hard time understanding why "I could just be using a new variable" follows from "it won't have a useful value".
- rnikander 4y agoAnd I forgot: 3. keyword arguments to functions. :)
- stargrazer 4y agoNot in the standard, but the language does support it: https://www.boost.org/doc/libs/1_79_0/libs/parameter/doc/html/index.html https://www.boost.org/doc/libs/1_79_0/libs/parameter/doc/htm...