diff --git a/BUCK b/BUCK index c6c3e85..43f2bb3 100644 --- a/BUCK +++ b/BUCK @@ -1,11 +1,15 @@ -cxx_library( +prebuilt_cxx_library( name = 'neither', + header_only = True, header_namespace = 'neither', exported_headers = subdir_glob([ ('neither/include', '**/*.hpp'), ]), + licenses = [ + 'LICENSE.txt', + ], visibility = [ - 'PUBLIC', + 'PUBLIC' ], ) @@ -14,9 +18,10 @@ cxx_test( srcs = glob([ 'neither/tests/**/*.cpp', ]), - platform_compiler_flags = [ - ('^linux.*', [ '-lpthread' ]), + platform_linker_flags = [ + ('^linux.*', [ '-lpthread', ]), ], + link_style = 'shared', deps = [ ':neither', ], diff --git a/license b/LICENSE.txt similarity index 100% rename from license rename to LICENSE.txt diff --git a/neither/include/maybe.hpp b/neither/include/maybe.hpp index 4353d22..a4c347b 100644 --- a/neither/include/maybe.hpp +++ b/neither/include/maybe.hpp @@ -2,6 +2,7 @@ #define NEITHER_MAYBE_HPP #include +#include #include namespace neither { @@ -38,9 +39,12 @@ template struct Maybe { } constexpr T get(T defaultValue) { - if (hasValue) - return value; - return defaultValue; + return hasValue ? value : defaultValue; + } + + constexpr T unsafeGet() { + assert(hasValue && "unsafeGet must not be called on an empty Maybe"); + return value; } template diff --git a/neither/tests/maybe.cpp b/neither/tests/maybe.cpp index c9c5f46..10456b8 100644 --- a/neither/tests/maybe.cpp +++ b/neither/tests/maybe.cpp @@ -28,6 +28,11 @@ TEST(neither, maybe_flatMap_no_value) { ASSERT_TRUE(x.flatMap([](auto x) { return maybe(); }).get(1) == 1); } +TEST(neither, maybe_unsafe_get_with_value) { + auto x = maybe(42); + ASSERT_TRUE(x.unsafeGet() == 42); +} + TEST(neither, maybe_comparison) { auto a = maybe(42);