44c3661b0063eb22e4e574498b926360519f2c7d
[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 // We will soon return Future<Unit> wherever we currently return Future<void>
26 // #6847876
27 struct Unit {
28   /// Lift type T into Unit. This is the definition for all non-void types.
29   template <class T> struct Lift : public std::false_type {
30     using type = T;
31   };
32   bool operator==(const Unit& other) const { return true; }
33   bool operator!=(const Unit& other) const { return false; }
34 };
35
36 // Lift void into Unit.
37 template <>
38 struct Unit::Lift<void> : public std::true_type {
39   using type = Unit;
40 };
41
42 // Lift Unit into Unit (identity).
43 template <>
44 struct Unit::Lift<Unit> : public std::true_type {
45   using type = Unit;
46 };
47
48 template <class T>
49 struct is_void_or_unit : public Unit::Lift<T>
50 {};
51
52 }