Skip to content

Commit a47cf35

Browse files
committed
Remove unnecessary ValueOptions
1 parent 34c6b1b commit a47cf35

8 files changed

Lines changed: 88 additions & 83 deletions

File tree

src/Fabulous/AttributeDefinitions.fs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,6 @@ module AttributeHelpers =
200200
open ScalarAttributeDefinitions
201201

202202
let tryFindSimpleScalarAttribute (definition: SimpleScalarAttributeDefinition<'T>) (widget: Widget) =
203-
match widget.ScalarAttributes with
204-
| ValueNone -> ValueNone
205-
| ValueSome attrs ->
206-
match attrs |> Array.tryFind(fun attr -> attr.Key = definition.Key) with
207-
| None -> ValueNone
208-
| Some attr -> ValueSome(unbox<'T> attr.Value)
203+
match widget.ScalarAttributes |> Array.tryFind(fun attr -> attr.Key = definition.Key) with
204+
| None -> ValueNone
205+
| Some attr -> ValueSome(unbox<'T> attr.Value)

src/Fabulous/Builders.fs

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ open Fabulous.StackAllocatedCollections
77
open Fabulous.StackAllocatedCollections.StackList
88
open Microsoft.FSharp.Core
99

10-
type AttributesBundle = (struct (StackList<ScalarAttribute> * WidgetAttribute[] voption * WidgetCollectionAttribute[] voption * EnvironmentAttribute[] voption))
10+
type AttributesBundle = (struct (StackList<ScalarAttribute> * WidgetAttribute[] * WidgetCollectionAttribute[] * EnvironmentAttribute[]))
1111

1212
[<Struct; NoComparison; NoEquality>]
1313
type WidgetBuilder<'msg, 'marker when 'msg: equality> =
@@ -17,25 +17,25 @@ type WidgetBuilder<'msg, 'marker when 'msg: equality> =
1717

1818
new(key: WidgetKey) =
1919
{ Key = key
20-
Attributes = AttributesBundle(StackList.empty(), ValueNone, ValueNone, ValueNone) }
20+
Attributes = AttributesBundle(StackList.empty(), [||], [||], [||]) }
2121

2222
new(key: WidgetKey, attributes: AttributesBundle) = { Key = key; Attributes = attributes }
2323

2424
new(key: WidgetKey, scalar: ScalarAttribute) =
2525
{ Key = key
26-
Attributes = AttributesBundle(StackList.one scalar, ValueNone, ValueNone, ValueNone) }
26+
Attributes = AttributesBundle(StackList.one scalar, [||], [||], [||]) }
2727

2828
new(key: WidgetKey, scalarA: ScalarAttribute, scalarB: ScalarAttribute) =
2929
{ Key = key
30-
Attributes = AttributesBundle(StackList.two(scalarA, scalarB), ValueNone, ValueNone, ValueNone) }
30+
Attributes = AttributesBundle(StackList.two(scalarA, scalarB), [||], [||], [||]) }
3131

3232
new(key: WidgetKey, scalar1: ScalarAttribute, scalar2: ScalarAttribute, scalar3: ScalarAttribute) =
3333
{ Key = key
34-
Attributes = AttributesBundle(StackList.three(scalar1, scalar2, scalar3), ValueNone, ValueNone, ValueNone) }
34+
Attributes = AttributesBundle(StackList.three(scalar1, scalar2, scalar3), [||], [||], [||]) }
3535

3636
new(key: WidgetKey, widget: WidgetAttribute) =
3737
{ Key = key
38-
Attributes = AttributesBundle(StackList.empty(), ValueSome [| widget |], ValueNone, ValueNone) }
38+
Attributes = AttributesBundle(StackList.empty(), [| widget |], [||], [||]) }
3939

4040
[<EditorBrowsable(EditorBrowsableState.Never)>]
4141
member x.Compile() : Widget =
@@ -48,14 +48,23 @@ type WidgetBuilder<'msg, 'marker when 'msg: equality> =
4848
#endif
4949
ScalarAttributes =
5050
match StackList.length &scalarAttributes with
51-
| 0us -> ValueNone
52-
| _ -> ValueSome(Array.sortInPlace _.Key (StackList.toArray &scalarAttributes))
51+
| 0us -> [||]
52+
| _ -> Array.sortInPlace _.Key (StackList.toArray &scalarAttributes)
5353

54-
WidgetAttributes = ValueOption.map (Array.sortInPlace(_.Key)) widgetAttributes
54+
WidgetAttributes =
55+
match widgetAttributes with
56+
| [||] -> [||]
57+
| _ -> Array.sortInPlace _.Key widgetAttributes
5558

56-
WidgetCollectionAttributes = widgetCollectionAttributes |> ValueOption.map(Array.sortInPlace(_.Key))
59+
WidgetCollectionAttributes =
60+
match widgetCollectionAttributes with
61+
| [||] -> [||]
62+
| _ -> Array.sortInPlace _.Key widgetCollectionAttributes
5763

58-
EnvironmentAttributes = environmentAttributes |> ValueOption.map(Array.sortInPlace(_.Key)) }
64+
EnvironmentAttributes =
65+
match environmentAttributes with
66+
| [||] -> [||]
67+
| _ -> Array.sortInPlace _.Key environmentAttributes }
5968

6069
[<EditorBrowsable(EditorBrowsableState.Never)>]
6170
member inline x.AddScalar(attr: ScalarAttribute) =
@@ -103,14 +112,14 @@ type WidgetBuilder<'msg, 'marker when 'msg: equality> =
103112

104113
let res =
105114
match attribs with
106-
| ValueNone -> [| attr |]
107-
| ValueSome attribs ->
115+
| [||] -> [| attr |]
116+
| attribs ->
108117
let attribs2 = Array.zeroCreate(attribs.Length + 1)
109118
Array.blit attribs 0 attribs2 0 attribs.Length
110119
attribs2[attribs.Length] <- attr
111120
attribs2
112121

113-
WidgetBuilder<'msg, 'marker>(x.Key, struct (scalarAttributes, ValueSome res, widgetCollectionAttributes, environmentAttributes))
122+
WidgetBuilder<'msg, 'marker>(x.Key, struct (scalarAttributes, res, widgetCollectionAttributes, environmentAttributes))
114123

115124
[<EditorBrowsable(EditorBrowsableState.Never)>]
116125
member x.AddWidgetCollection(attr: WidgetCollectionAttribute) =
@@ -121,14 +130,14 @@ type WidgetBuilder<'msg, 'marker when 'msg: equality> =
121130

122131
let res =
123132
match attribs with
124-
| ValueNone -> [| attr |]
125-
| ValueSome attribs ->
133+
| [||] -> [| attr |]
134+
| attribs ->
126135
let attribs2 = Array.zeroCreate(attribs.Length + 1)
127136
Array.blit attribs 0 attribs2 0 attribs.Length
128137
attribs2[attribs.Length] <- attr
129138
attribs2
130139

131-
WidgetBuilder<'msg, 'marker>(x.Key, struct (scalarAttributes, widgetAttributes, ValueSome res, environmentAttributes))
140+
WidgetBuilder<'msg, 'marker>(x.Key, struct (scalarAttributes, widgetAttributes, res, environmentAttributes))
132141

133142
[<EditorBrowsable(EditorBrowsableState.Never)>]
134143
member inline x.AddEnvironment(key: EnvironmentAttributeKey, value: obj) =
@@ -146,14 +155,14 @@ type WidgetBuilder<'msg, 'marker when 'msg: equality> =
146155

147156
let res =
148157
match attribs with
149-
| ValueNone -> [| attr |]
150-
| ValueSome attribs ->
158+
| [||] -> [| attr |]
159+
| attribs ->
151160
let attribs2 = Array.zeroCreate(attribs.Length + 1)
152161
Array.blit attribs 0 attribs2 0 attribs.Length
153162
attribs2[attribs.Length] <- attr
154163
attribs2
155164

156-
WidgetBuilder<'msg, 'marker>(x.Key, struct (scalarAttributes, widgetAttributes, widgetCollectionAttributes, ValueSome res))
165+
WidgetBuilder<'msg, 'marker>(x.Key, struct (scalarAttributes, widgetAttributes, widgetCollectionAttributes, res))
157166
end
158167

159168

@@ -170,12 +179,12 @@ type CollectionBuilder<'msg, 'marker, 'itemMarker when 'msg: equality> =
170179

171180
new(widgetKey: WidgetKey, scalars: StackList<ScalarAttribute>, attr: WidgetCollectionAttributeDefinition) =
172181
{ WidgetKey = widgetKey
173-
Attributes = AttributesBundle(scalars, ValueNone, ValueNone, ValueNone)
182+
Attributes = AttributesBundle(scalars, [||], [||], [||])
174183
Attr = attr }
175184

176185
new(widgetKey: WidgetKey, attr: WidgetCollectionAttributeDefinition) =
177186
{ WidgetKey = widgetKey
178-
Attributes = AttributesBundle(StackList.empty(), ValueNone, ValueNone, ValueNone)
187+
Attributes = AttributesBundle(StackList.empty(), [||], [||], [||])
179188
Attr = attr }
180189

181190
new(widgetKey: WidgetKey, attr: WidgetCollectionAttributeDefinition, attributes: AttributesBundle) =
@@ -185,12 +194,12 @@ type CollectionBuilder<'msg, 'marker, 'itemMarker when 'msg: equality> =
185194

186195
new(widgetKey: WidgetKey, attr: WidgetCollectionAttributeDefinition, scalar: ScalarAttribute) =
187196
{ WidgetKey = widgetKey
188-
Attributes = AttributesBundle(StackList.one scalar, ValueNone, ValueNone, ValueNone)
197+
Attributes = AttributesBundle(StackList.one scalar, [||], [||], [||])
189198
Attr = attr }
190199

191200
new(widgetKey: WidgetKey, attr: WidgetCollectionAttributeDefinition, scalarA: ScalarAttribute, scalarB: ScalarAttribute) =
192201
{ WidgetKey = widgetKey
193-
Attributes = AttributesBundle(StackList.two(scalarA, scalarB), ValueNone, ValueNone, ValueNone)
202+
Attributes = AttributesBundle(StackList.two(scalarA, scalarB), [||], [||], [||])
194203
Attr = attr }
195204

196205
member inline x.Run(c: Content<'msg>) =
@@ -205,8 +214,8 @@ type CollectionBuilder<'msg, 'marker, 'itemMarker when 'msg: equality> =
205214

206215
let widgetCollections =
207216
match widgetCollections with
208-
| ValueNone -> ValueSome([| widgetCollAttr |])
209-
| ValueSome widgetCollectionAttributes -> ValueSome(Array.appendOne widgetCollAttr widgetCollectionAttributes)
217+
| [||] -> [| widgetCollAttr |]
218+
| widgetCollectionAttributes -> Array.appendOne widgetCollAttr widgetCollectionAttributes
210219

211220
WidgetBuilder<'msg, 'marker>(x.WidgetKey, AttributesBundle(scalars, widgets, widgetCollections, environments))
212221

src/Fabulous/Components/Component.fs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,40 @@ type Component
3333
| ValueSome componentWidget ->
3434
let componentScalars =
3535
match componentWidget.ScalarAttributes with
36-
| ValueNone -> ValueNone
37-
| ValueSome attrs ->
36+
| [||] -> [||]
37+
| attrs ->
3838
let filteredAttrs =
3939
attrs |> Array.filter(fun scalarAttr -> scalarAttr.Key <> componentDataKey)
4040

41-
ValueSome(filteredAttrs) // skip the component data
41+
filteredAttrs // skip the component data
4242

4343
let scalars =
4444
match rootWidget.ScalarAttributes, componentScalars with
45-
| ValueNone, ValueNone -> ValueNone
46-
| ValueSome attrs, ValueNone
47-
| ValueNone, ValueSome attrs -> ValueSome attrs
48-
| ValueSome widgetAttrs, ValueSome componentAttrs -> ValueSome(Array.append componentAttrs widgetAttrs)
45+
| [||], [||] -> [||]
46+
| attrs, [||]
47+
| [||], attrs -> attrs
48+
| widgetAttrs, componentAttrs -> Array.append componentAttrs widgetAttrs
4949

5050
let widgets =
5151
match rootWidget.WidgetAttributes, componentWidget.WidgetAttributes with
52-
| ValueNone, ValueNone -> ValueNone
53-
| ValueSome attrs, ValueNone
54-
| ValueNone, ValueSome attrs -> ValueSome attrs
55-
| ValueSome widgetAttrs, ValueSome componentAttrs -> ValueSome(Array.append componentAttrs widgetAttrs)
52+
| [||], [||] -> [||]
53+
| attrs, [||]
54+
| [||], attrs -> attrs
55+
| widgetAttrs, componentAttrs -> Array.append componentAttrs widgetAttrs
5656

5757
let widgetColls =
5858
match rootWidget.WidgetCollectionAttributes, componentWidget.WidgetCollectionAttributes with
59-
| ValueNone, ValueNone -> ValueNone
60-
| ValueSome attrs, ValueNone
61-
| ValueNone, ValueSome attrs -> ValueSome attrs
62-
| ValueSome widgetAttrs, ValueSome componentAttrs -> ValueSome(Array.append componentAttrs widgetAttrs)
59+
| [||], [||] -> [||]
60+
| attrs, [||]
61+
| [||], attrs -> attrs
62+
| widgetAttrs, componentAttrs -> Array.append componentAttrs widgetAttrs
6363

6464
let environments =
6565
match rootWidget.EnvironmentAttributes, componentWidget.EnvironmentAttributes with
66-
| ValueNone, ValueNone -> ValueNone
67-
| ValueSome attrs, ValueNone
68-
| ValueNone, ValueSome attrs -> ValueSome attrs
69-
| ValueSome widgetAttrs, ValueSome componentAttrs -> ValueSome(Array.append componentAttrs widgetAttrs)
66+
| [||], [||] -> [||]
67+
| attrs, [||]
68+
| [||], attrs -> attrs
69+
| widgetAttrs, componentAttrs -> Array.append componentAttrs widgetAttrs
7070

7171
struct (scalars, widgets, widgetColls, environments)
7272

src/Fabulous/Components/Widget.fs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ module Component' =
1616
CreateView =
1717
fun (widget, envContext, treeContext, _) ->
1818
match widget.ScalarAttributes with
19-
| ValueNone -> failwith "Component widget must have a body"
20-
| ValueSome attrs ->
19+
| [||] -> failwith "Component widget must have a body"
20+
| attrs ->
2121
let data =
2222
let scalarAttrsOpt =
2323
attrs |> Array.tryFind(fun scalarAttr -> scalarAttr.Key = Data.Key)
@@ -37,8 +37,8 @@ module Component' =
3737
AttachView =
3838
fun (widget, envContext, treeContext, _, view) ->
3939
match widget.ScalarAttributes with
40-
| ValueNone -> failwith "Component widget must have a body"
41-
| ValueSome attrs ->
40+
| [||] -> failwith "Component widget must have a body"
41+
| attrs ->
4242
let data =
4343
let scalarAttrsOpt =
4444
attrs |> Array.tryFind(fun scalarAttr -> scalarAttr.Key = Data.Key)
@@ -62,26 +62,25 @@ module Component' =
6262
let canReuseComponent (prev: Widget) (curr: Widget) =
6363
let prevData =
6464
match prev.ScalarAttributes with
65-
| ValueSome attrs ->
65+
| [||] -> failwith "Component widget must have a body"
66+
| attrs ->
6667
let scalarAttrsOpt =
6768
attrs |> Array.tryFind(fun scalarAttr -> scalarAttr.Key = Data.Key)
6869

6970
match scalarAttrsOpt with
7071
| None -> failwithf "Component widget must have a body"
7172
| Some value -> value.Value :?> ComponentData
7273

73-
| _ -> failwith "Component widget must have a body"
74-
7574
let currData =
7675
match curr.ScalarAttributes with
77-
| ValueSome attrs ->
76+
| [||] -> failwith "Component widget must have a body"
77+
| attrs ->
7878
let scalarAttrsOpt =
7979
attrs |> Array.tryFind(fun scalarAttr -> scalarAttr.Key = Data.Key)
8080

8181
match scalarAttrsOpt with
8282
| None -> failwithf "Component widget must have a body"
8383
| Some value -> value.Value :?> ComponentData
84-
| _ -> failwith "Component widget must have a body"
8584

8685
// NOTE: Somehow using = here crashes the app and prevents debugging...
8786
Object.Equals(prevData.Key, currData.Key)

src/Fabulous/Dispatch.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ module Dispatcher =
4646
| ValueSome msg -> dispatch msg
4747

4848
match widget.WidgetAttributes with
49-
| ValueNone -> ()
50-
| ValueSome widgetAttrs ->
49+
| [||] -> ()
50+
| widgetAttrs ->
5151
for childAttr in widgetAttrs do
5252
dispatchAndVisitChildren false dispatch childAttr.Value
5353

5454
match widget.WidgetCollectionAttributes with
55-
| ValueNone -> ()
56-
| ValueSome widgetCollAttrs ->
55+
| [||] -> ()
56+
| widgetCollAttrs ->
5757
for widgetCollAttr in widgetCollAttrs do
5858
for childWidget in ArraySlice.toSpan widgetCollAttr.Value do
5959
dispatchAndVisitChildren false dispatch childWidget

src/Fabulous/Memo.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module Memo =
5555

5656
let inline private getMemoData (widget: Widget) : MemoData =
5757
match widget.ScalarAttributes with
58-
| ValueSome attrs when attrs.Length = 1 -> attrs[0].Value :?> MemoData
58+
| [| attr |] -> attr.Value :?> MemoData
5959
| _ -> failwith "Memo widget cannot have extra attributes"
6060

6161
let internal canReuseMemoizedWidget prev next =

src/Fabulous/Primitives.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ and [<Struct>] Widget =
137137
#if DEBUG
138138
DebugName: string
139139
#endif
140-
ScalarAttributes: ScalarAttribute[] voption
141-
WidgetAttributes: WidgetAttribute[] voption
142-
WidgetCollectionAttributes: WidgetCollectionAttribute[] voption
143-
EnvironmentAttributes: EnvironmentAttribute[] voption }
140+
ScalarAttributes: ScalarAttribute[]
141+
WidgetAttributes: WidgetAttribute[]
142+
WidgetCollectionAttributes: WidgetCollectionAttribute[]
143+
EnvironmentAttributes: EnvironmentAttribute[] }

0 commit comments

Comments
 (0)