Skip to content

Commit

Permalink
Merge pull request #17 from LoopPerfect/p15
Browse files Browse the repository at this point in the history
P15
  • Loading branch information
nikhedonia authored Oct 12, 2017
2 parents 9cd1405 + f5208df commit a2bbaa2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
13 changes: 9 additions & 4 deletions BUCK
Original file line number Diff line number Diff line change
@@ -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'
],
)

Expand All @@ -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',
],
Expand Down
File renamed without changes.
10 changes: 7 additions & 3 deletions neither/include/maybe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define NEITHER_MAYBE_HPP

#include <memory>
#include <cassert>
#include <neither/traits.hpp>

namespace neither {
Expand Down Expand Up @@ -38,9 +39,12 @@ template <class T> 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<class F>
Expand Down
5 changes: 5 additions & 0 deletions neither/tests/maybe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ TEST(neither, maybe_flatMap_no_value) {
ASSERT_TRUE(x.flatMap([](auto x) { return maybe<int>(); }).get(1) == 1);
}

TEST(neither, maybe_unsafe_get_with_value) {
auto x = maybe<int>(42);
ASSERT_TRUE(x.unsafeGet() == 42);
}

TEST(neither, maybe_comparison) {

auto a = maybe<int>(42);
Expand Down

0 comments on commit a2bbaa2

Please sign in to comment.