#Optionals

Pixelcode Apps :verified:​pixelcodeapps@mstdn.social
2025-06-17

#Optionals are so tedious to deal with 😮‍💨”

You know what's more tedious? #NullPointerException‎s

Uli Kusterer (Not a kitteh)uliwitness@chaos.social
2025-04-02

I wonder if it'd work to add a guaranteed non-null pointer to C++ like this:

gist.github.com/uliwitness/931

#CPlusPlus #optionals #shared_ptr

Update: The gist is now fully compiling code.

/*  Since shared_ptr may always be nullptr, it already is an optional.
    But that means we need a non-nullable version of shared_ptr.
    
    Prerequisites: 1. Can't be assigned a null value
                   2. Can be assigned a non-null
                   3. Can be copied to a shared_ptr or another nonoptional_shared_ptr
    
    Would probably also need nonoptional_shared_ptr-returning versions of make_shared<T> etc.
*/

using namespace std;

template<class T>
class nonoptional_shared_ptr {
public:
    nonoptional_shared_ptr(const nonoptional_shared_ptr<T>& p) : _ptr(p._ptr) {}
    nonoptional_shared_ptr<T>& operator= (const nonoptional_shared_ptr<T>& p) { _ptr = p._ptr; }

    operator shared_ptr<T> () { return _ptr; }
    
    T* get() { return _ptr.get(); }
    T* operator -> () { return _ptr.get(); }
    T& operator * () { return *_ptr; }

    // Add more methods here that forward shared_ptr methods so it can be used same way.
  
protected:
    nonoptional_shared_ptr(const shared_ptr<T>& p) : _ptr(p) {}
    shared_ptr<T> _ptr;
  
    friend void if_nonnullptr(shared_ptr<T> possiblyNull, function<void(nonoptional_shared_ptr<T>)> nonnullHandler, function<void()> nullHandler);
};

void if_nonnullptr(shared_ptr<T> possiblyNull, function<void(nonoptional_shared_ptr<T>)> nonnullHandler, function<void()> nullHandler) {
    if (possiblyNull) {
        nonnullHandler(nonoptional_shared_ptr(possiblyNull));
    } else {
        nullHandler();
    }
}class Foo {
public:
  
    Foo(const nonoptional_shared_ptr& p) : _neverNull(p) {}
  
protected:
    nonoptional_shared_ptr _neverNull;
};

int main(int argc, const char** argv) {
    shared_ptr<Bar> bar = make_shared<Bar>();
    shared_ptr<Foo> dest;
  
    if_nonnullptr(bar,
                  [&dest](const nonoptional_shared_ptr& p) {
                      dest = make_shared<Foo>(p);
                  },
                  [](){
                      cerr << "Error!" << endl;
                  });
    
    // Now we have an (optional) Foo, but if it exists, we know that its _neverNull is a valid pointer.
    
    return EXIT_SUCCESS;
}
2024-04-07

Dear Friends of Options, :blobheart:

My brain (pah!) #partially #works in #instances of #sprouting. I #honour its #capacity by:

- #Random acts of #spurt
- #Forging a head, or even ahead
- Never underestimating advice I have not heard :mindblown:
- Believe in council :thinkSpin:
- It has been solved, somewhere
- The Internet is your friend?
- Walk placidly, like a cat :blobcatghostdead:
- Pirate, no parrot? Shoulder cat, hobble along
- Tea from? Nettles, #mint, peal, spices?
- Work WITH. Shout again, WITH nature
- Does not work, may not be needed
- Know Brain, no gain, Know Citizen Banned

Hope that aids your #optionals

Client Info

Server: https://mastodon.social
Version: 2025.04
Repository: https://github.com/cyevgeniy/lmst