folly::Unit::Drop.
[folly.git] / folly / futures / Unit.h
1 /*
2  * Copyright 2015 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 #pragma once
17 namespace folly {
18
19 /// In functional programming, the degenerate case is often called "unit". In
20 /// C++, "void" is often the best analogue, however because of the syntactic
21 /// special-casing required for void it is a liability for template
22 /// metaprogramming. So, instead of e.g. Future<void>, we have Future<Unit>.
23 /// You can ignore the actual value, and we port some of the syntactic
24 /// niceties like setValue() instead of setValue(Unit{}).
25 struct Unit {
26   /// Lift type T into Unit. This is the definition for all non-void types.
27   template <class T> struct Lift : public std::false_type {
28     using type = T;
29   };
30   template <class T> struct Drop : public std::false_type {
31     using type = T;
32   };
33   bool operator==(const Unit& other) const { return true; }
34   bool operator!=(const Unit& other) const { return false; }
35 };
36
37 // Lift void into Unit.
38 template <>
39 struct Unit::Lift<void> : public std::true_type {
40   using type = Unit;
41 };
42
43 // Lift Unit into Unit (identity).
44 template <>
45 struct Unit::Lift<Unit> : public std::true_type {
46   using type = Unit;
47 };
48
49 // Drop Unit into void.
50 template <>
51 struct Unit::Drop<Unit> : public std::true_type {
52   using type = void;
53 };
54
55 // Drop void into void (identity).
56 template <>
57 struct Unit::Drop<void> : public std::true_type {
58   using type = void;
59 };
60
61 template <class T>
62 struct is_void_or_unit : public Unit::Lift<T>
63 {};
64
65 }