Skip to content

Commit a2bbaa2

Browse files
authored
Merge pull request #17 from LoopPerfect/p15
P15
2 parents 9cd1405 + f5208df commit a2bbaa2

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

BUCK

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
cxx_library(
1+
prebuilt_cxx_library(
22
name = 'neither',
3+
header_only = True,
34
header_namespace = 'neither',
45
exported_headers = subdir_glob([
56
('neither/include', '**/*.hpp'),
67
]),
8+
licenses = [
9+
'LICENSE.txt',
10+
],
711
visibility = [
8-
'PUBLIC',
12+
'PUBLIC'
913
],
1014
)
1115

@@ -14,9 +18,10 @@ cxx_test(
1418
srcs = glob([
1519
'neither/tests/**/*.cpp',
1620
]),
17-
platform_compiler_flags = [
18-
('^linux.*', [ '-lpthread' ]),
21+
platform_linker_flags = [
22+
('^linux.*', [ '-lpthread', ]),
1923
],
24+
link_style = 'shared',
2025
deps = [
2126
':neither',
2227
],
File renamed without changes.

neither/include/maybe.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define NEITHER_MAYBE_HPP
33

44
#include <memory>
5+
#include <cassert>
56
#include <neither/traits.hpp>
67

78
namespace neither {
@@ -38,9 +39,12 @@ template <class T> struct Maybe {
3839
}
3940

4041
constexpr T get(T defaultValue) {
41-
if (hasValue)
42-
return value;
43-
return defaultValue;
42+
return hasValue ? value : defaultValue;
43+
}
44+
45+
constexpr T unsafeGet() {
46+
assert(hasValue && "unsafeGet must not be called on an empty Maybe");
47+
return value;
4448
}
4549

4650
template<class F>

neither/tests/maybe.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ TEST(neither, maybe_flatMap_no_value) {
2828
ASSERT_TRUE(x.flatMap([](auto x) { return maybe<int>(); }).get(1) == 1);
2929
}
3030

31+
TEST(neither, maybe_unsafe_get_with_value) {
32+
auto x = maybe<int>(42);
33+
ASSERT_TRUE(x.unsafeGet() == 42);
34+
}
35+
3136
TEST(neither, maybe_comparison) {
3237

3338
auto a = maybe<int>(42);

0 commit comments

Comments
 (0)