| 1 | # Option/Sum Types for Qt Projects |
| 2 | |
| 3 | A set of simple(ish) C++ templates which implement [sum](http://en.wikipedia.org/wiki/Sum_type) and [option](http://en.wikipedia.org/wiki/Option_type) |
| 4 | types. They serve a similar purpose to `boost::variant` and `boost::optional` |
| 5 | but are implemented on top of Qt's QVariant container. |
| 6 | |
| 7 | ## Templates |
| 8 | |
| 9 | ### Either |
| 10 | |
| 11 | `Either<T1,T2>` represents either a value of type `T1` or |
| 12 | a value of type `T2`. You can think of it as a QVariant which is restricted |
| 13 | to being either a `T1` or `T2` rather than any type which QVariant supports. |
| 14 | |
| 15 | `Either<T1,T2>` instances can be constructed directly from an instance |
| 16 | of `T1` or `T2` or by using the `some(T)` function which returns a type that |
| 17 | can be implicitly cast to any `Either` type that has `T` as one of its |
| 18 | types. |
| 19 | |
| 20 | Simple example: |
| 21 | |
| 22 | // parse a value which might either be a |
| 23 | // string or an integer |
| 24 | |
| 25 | Either<QString,int> value = parseValue(data); |
| 26 | if (value.is1st()) { |
| 27 | qDebug() << "Read string " << value.as1st(); |
| 28 | } else { |
| 29 | qDebug() << "Read integer " << value.as2nd(); |
| 30 | } |
| 31 | |
| 32 | ### Maybe |
| 33 | |
| 34 | `Maybe<T>` represents either a value of type T or no value. You can think of it |
| 35 | as a QVariant which is restricted to either being a T or a null type. |
| 36 | |
| 37 | `Maybe<T>` instances are constructed using the `just(T)` or `nothing()` |
| 38 | functions. |
| 39 | |
| 40 | `just(T)` handles pointers specially. If passed a null pointer it returns |
| 41 | a `Maybe` representing no value. This is useful when working with existing code |
| 42 | which uses a null pointer to represent no value. |
| 43 | |
| 44 | Simple example: |
| 45 | |
| 46 | // try to read the size of a file, which may fail if |
| 47 | // the file is not readable |
| 48 | |
| 49 | Maybe<int> size = fileSize(path); |
| 50 | if (size) { |
| 51 | std::cout << "file size is " << size.value() << " bytes" << std::endl; |
| 52 | } else { |
| 53 | std::cout << "could not read file size" << std::endl; |
| 54 | } |
| 55 | |
| 56 | ## Examples |
| 57 | |
| 58 | See `TestMaybe.cpp` for unit tests which show example usage of the `Maybe` and |
| 59 | `Either` templates. |
| 60 | |
| 61 | ## Performance |
| 62 | |
| 63 | The templates use `QVariant` as the underlying storage and the same performance considerations apply. A QVariant in Qt 4 is 12 bytes in size on most platforms - 8 bytes for the data itself or a pointer to the data, plus 4 bytes for the type tag and flags. Pointers and primitive types of 8 bytes in size or less are stored within the QVariant. Larger types are stored in heap-allocated storage. |
| 64 | |
| 65 | ## Exceptions |
| 66 | |
| 67 | Unlike `boost::variant`, no attempt is made to [handle exceptions](http://www.boost.org/doc/libs/1_48_0/doc/html/variant/design.html#variant.design.never-empty) that may be thrown when constructing or copying types. The state of a `Maybe` or `Either` after a failed copy constructor call is undefined. |