|
|
|
|
|
by rnikander
1513 days ago
|
|
Years 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. |
|
E.g. in
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