@@ -7,103 +7,58 @@ Option to replace Nullable as in functional programming for Unity.
7
7
- [ Serialization] ( #serialization )
8
8
9
9
# Getting started
10
+
11
+ ### Initialize
10
12
``` csharp
11
- using Corby .Option ;
13
+ Option < int > num = new (); // None<int>
14
+ Option < int > num = 3 .ToSome (); // Some<>(3)
15
+ Option < Transform > parent = transform .parent .ToOption (); // Some<> or None
16
+ ```
12
17
13
- void Func ()
14
- {
15
- // Default usage
16
- {
17
- string helloString = " Hello" ;
18
- string worldString = null ;
19
-
20
- var a = helloString .Some (); // Some<String>("Hello")
21
- var b = worldString .Some (); // None<String>() because worldString is null
22
- var c = " Option" .Some (); // Some<String>("Option") by literal
23
-
24
- var printString = a switch
25
- {
26
- Some <string > some => some .Value ,
27
- None <string > none => " None"
28
- };
29
-
30
- UnityEngine .Debug .log (printString ); // print "Hello"
31
- UnityEngine .Debug .log (a ); // print "Some<string>(Hello)"
32
- UnityEngine .Debug .log (b ); // print "None<string>"
33
- }
34
-
35
-
36
- // safety usage without null exception for function
37
- {
38
- int [] array1 = new int [] { 1 , 2 , 3 };
39
- int [] array2 = null ;
40
-
41
- Func < int [], Option < int >> GetLength = (array ) =>
42
- {
43
- if (array == null ) return None <int >.New ;
44
- return array .Length .Some ();
45
- }
46
-
47
- UnityEngine.Debug.log($"length: {GetLength (array1 )}"); // print "length: Some<int>(3)"
48
- UnityEngine .Debug .log ($" length: {GetLength (array2 )}" ); // print "length: None<int>"
49
- }
50
-
51
-
52
- // Four basic operations
53
- {
54
- int a = 3 ;
55
- Option < int > b = 7 .Some ();
56
- Option < int > c = None <int >.New ;
57
-
58
- // Option<int> is change to int whan calculated
59
- UnityEngine .Debug .log ($" {a .Add (b )}" ); // print "a: 10"
60
- UnityEngine .Debug .log ($" {a .Sub (c )}" ); // print "a: 3" because None isn't calculated
61
- UnityEngine .Debug .log ($" {b .Mul (a )}" ); // print "a: 21"
62
- UnityEngine .Debug .log ($" {b .Div (c )}" ); // print "a: 7" because None isn't calculated
63
- }
18
+ ### Return without null
19
+ ``` csharp
20
+ public static Option < int > GetParent <T >(this Transform transform )
21
+ {
22
+ return transform .parent .ToOption ();
64
23
}
65
24
```
66
25
26
+ ### Unwarpping
27
+ ``` csharp
28
+ var parentName = transform .GetParent ().Unwrap () switch
29
+ {
30
+ Some <Transform > parent => parent .name ,
31
+ None <Transform > => " None"
32
+ };
33
+ ```
34
+
35
+
36
+
67
37
# Serialization
68
- ![ image] ( https://github.com/CorbyO/Option/assets/17669733/c6e738f0-28be-4257-948e-a7c8cdf2ba2d )
69
- This is a simple example of how to serialize a class or struct that contains Option.
70
- i don't found serialize for interface so you want to serialize the option, you use the SerializableOption class .
38
+ ![ image] ( https://github.com/CorbyO/Option/assets/17669733/6be717ce-b181-4b86-9238-a5d00fa900b4 )
39
+
40
+ This is a simple example of how to serialize a class or struct that contains Option .
71
41
72
42
``` csharp
73
- // good
74
43
[Serializable ]
75
44
public struct TestStruct
76
45
{
77
- public SerializableOption <Vector3 > OptionVector3 ;
78
- public SerializableOption <Quaternion > OptionQuaternion ;
79
- public SerializableOption <Transform > OptionTransform ;
80
- }
81
-
82
- // bad: Serialize is fail because [Serializable] is not applied.
83
- public struct TestStruct2
84
- {
85
- public SerializableOption <int > i ;
46
+ public Option <int > Number ;
47
+ public Option <string > Name ;
48
+ public Option <Vector3 > Position ;
86
49
}
87
50
88
- // good
89
51
[Serializable ]
90
52
public class TestClass
91
53
{
92
- public SerializableOption <int > OptionInt ;
93
- public SerializableOption <float > OptionFloat ;
94
- public SerializableOption <string > OptionString ;
95
- public SerializableOption <TestStruct > OptionTestStruct ;
96
- public SerializableOption <TestStruct2 > OptionTestStruct2 ;
54
+ public Option <Sprite > Sprite ;
55
+ public Option <List <int >> Numbers ;
97
56
}
98
57
99
58
100
59
public class TestComponent : MonoBehaviour
101
60
{
102
- public SerializableOption <TestClass > OptionTestClass ;
103
-
104
- void Awake ()
105
- {
106
- var option = OptionTestClass .Get (); // Some<TestClass> or None<TestClass>
107
- }
61
+ public Option <TestStruct > Struct ;
62
+ public Option <TestClass > Class ;
108
63
}
109
64
```
0 commit comments