cb6a6566df41a57ead7ea60d940e338f639bc61c
[folly.git] / folly / Unit.h
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18
19 #include <type_traits>
20
21 namespace folly {
22
23 /// In functional programming, the degenerate case is often called "unit". In
24 /// C++, "void" is often the best analogue, however because of the syntactic
25 /// special-casing required for void it is a liability for template
26 /// metaprogramming. So, instead of e.g. Future<void>, we have Future<Unit>.
27 /// You can ignore the actual value, and we port some of the syntactic
28 /// niceties like setValue() instead of setValue(Unit{}).
29 struct Unit {
30   template <typename T>
31   using Lift = std::conditional<std::is_same<T, void>::value, Unit, T>;
32   template <typename T>
33   using Drop = std::conditional<std::is_same<T, Unit>::value, void, T>;
34
35   bool operator==(const Unit& /*other*/) const { return true; }
36   bool operator!=(const Unit& /*other*/) const { return false; }
37 };
38
39 constexpr Unit unit {};
40
41 }