@@ -34,35 +34,117 @@ struct Option(T)
3434 }
3535 }
3636
37- // is None ?
37+
38+ /**
39+ Сhecks if Option has None (i.e empty, null etc.) value
40+ Returns:
41+ Returns true if the option is None, false otherwise.
42+
43+ Typical usage:
44+ ----
45+ import std.stdio : writeln;
46+
47+ Option!int value;
48+ writeln(value.isNone) // true
49+ ----
50+ */
3851 bool isNone () {
3952 return type == OptionType.NONE ;
4053 }
4154
42- // is Some ?
55+
56+ /**
57+ Сhecks if Option has value (i.e empty, null etc.) value
58+ Returns:
59+ Returns true if the option holds value, false otherwise.
60+
61+ Typical usage:
62+ ----
63+ import std.stdio : writeln;
64+
65+ Option!int value;
66+ value.Some(10)
67+ writeln(value.isSome) // true
68+ ----
69+ */
4370 bool isSome () {
4471 return type == OptionType.SOME ;
4572 }
4673
47- // return Option if Some or return defaultValue otherwise
74+
75+ /**
76+ A method that gets the value if the Option is Some, or return defaultValue otherwise.
77+ Returns:
78+ Returns the value in Option or default value.
79+
80+ Typical usage:
81+ ----
82+ Option!int x;
83+ writeln(x.orElse()); // Prints: 10
84+ ----
85+ */
4886 T orElse (T defaultValue)
4987 {
5088 return (type == OptionType.NONE ) ? defaultValue : value;
5189 }
5290
53- // None type
91+
92+ /**
93+ Сreate None (i.e empty, null etc.) value, packed in Option!T
94+ Returns:
95+ Returns Option!T with None value for given type T.
96+
97+ Typical usage:
98+ ----
99+ import std.stdio : writeln;
100+
101+ // without using None method gives default None value
102+ Option!int value;
103+ // ditto
104+ value = value.None();
105+ ----
106+ */
54107 Option! T None ()
55108 {
56109 return Option! T(OptionType.NONE );
57110 }
58111
59- // Some value
112+
113+ /**
114+ Сreate non-null or non-empty value, packed in Option!T
115+ Returns:
116+ Returns Option!T with packed value for given type T.
117+
118+ Typical usage:
119+ ----
120+ import std.stdio : writeln;
121+
122+ // without using None method gives default None value
123+ Option!int value;
124+ // set value in Option!int
125+ value = value.Some(5);
126+ ----
127+ */
60128 Option! T Some (T value)
61129 {
62130 return Option! T(OptionType.SOME , value);
63131 }
64132
65- // string representation
133+
134+ /**
135+ A string representation of a Option type.
136+ The toString() method output Option type as a string in the following format: Some(value) or None()
137+ Returns:
138+ String representation for Option type.
139+
140+ Typical usage:
141+ ----
142+ import std.stdio : writeln;
143+
144+ Option!string s;
145+ s.writeln; // "None"
146+ ----
147+ */
66148 string toString () const
67149 {
68150 import std.conv : to;
@@ -71,7 +153,19 @@ struct Option(T)
71153 return (type == OptionType.NONE ) ? " None" : format(" Some(%s)" , to! string (value));
72154 }
73155
74- // unwrap value
156+
157+ /**
158+ A method to unwrap the value of the Option.
159+ Returns:
160+ Returns the value stored in Option if Option itself does not consider None.
161+
162+ Typical usage:
163+ ----
164+ Option!int x;
165+ x = x.Some(10);
166+ writeln(x.unwrap()); // Prints: 10
167+ ----
168+ */
75169 T unwrap () const
76170 {
77171 if (type == OptionType.SOME )
0 commit comments