Skip to content

Commit 9468428

Browse files
committed
Let Flutter FFI declare const constructors
As `const` constructors are guaranteed not to have side-effects thus not requiring `IO`.
1 parent fce8ad6 commit 9468428

File tree

7 files changed

+60
-60
lines changed

7 files changed

+60
-60
lines changed

examples/flutterdoodle/lib/Main.idr

+11-11
Original file line numberDiff line numberDiff line change
@@ -46,37 +46,37 @@ onPaint s c _ = case s of
4646
appTitle : String
4747
appTitle = "Try taps and long presses"
4848

49-
appHome : IO Stateful
49+
appHome : Stateful
5050
appHome = Stateful.new [initialState @= Idle [], onBuild @= build]
5151
where
5252
build : StatefulWidgetState State -> BuildContext -> IO Widget
53-
build state context = cast <$> Scaffold.new [
53+
build state context = widget $ Scaffold.new [
5454
appBar @=> !(AppBar.new [
55-
title @=> !(Text.new appTitle [])
55+
title @=> Text.new appTitle []
5656
]),
57-
body @=> !(Center.new [
58-
child @=> !(CustomPaint.new [
57+
body @=> Center.new [
58+
child @=> CustomPaint.new [
5959
child @=> !(GestureDetector.new [
6060
onTapUp @= modify state . onTapUp,
6161
onLongPressStart @= modify state . onLongPressStart,
6262
onLongPressMoveUpdate @= modify state . onLongPressMoveUpdate,
6363
onLongPressEnd @= modify state . onLongPressEnd
6464
]),
65-
painter @=> !(Painter.new [
65+
painter @=> Painter.new [
6666
onPaint @= onPaint (get state)
67-
])
68-
])
69-
])
67+
]
68+
]
69+
]
7070
]
7171

7272
app : IO MaterialApp
73-
app = MaterialApp.new [
73+
app = pure $ MaterialApp.new [
7474
title @= appTitle,
7575
theme @= !(ThemeData.new [
7676
primarySwatch @= Colors.blue,
7777
visualDensity @= VisualDensity.adaptivePlatformDensity
7878
]),
79-
home @=> !appHome
79+
home @=> appHome
8080
]
8181

8282
main : IO ()

examples/fluttertemplate/lib/Main.idr

+16-16
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,39 @@ import Flutter
55
appTitle : String
66
appTitle = "Idris Demo Home Page"
77

8-
appHome : IO Stateful
8+
appHome : Stateful
99
appHome = Stateful.new [initialState @= 0, onBuild @= build]
1010
where
1111
build : StatefulWidgetState Int -> BuildContext -> IO Widget
12-
build state context = cast <$> Scaffold.new [
12+
build state context = widget $ Scaffold.new [
1313
appBar @=> !(AppBar.new [
14-
title @=> !(Text.new appTitle [])
14+
title @=> Text.new appTitle []
1515
]),
16-
body @=> !(Center.new [
16+
body @=> Center.new [
1717
child @=> !(Column.new [
1818
mainAxisAlignment @= MainAxisAlignment.center,
1919
children @= widgets [
20-
!(Text.new "You have pushed the button this many times:" []),
21-
!(Text.new (show (get state)) [
22-
style @= headline4 (textTheme !(Theme.of context))
23-
])
20+
Text.new "You have pushed the button this many times:" [],
21+
Text.new (show (get state)) [
22+
style @= !(Theme.of context) @. textTheme @. headline4
23+
]
2424
]
2525
])
26-
]),
27-
floatingActionButton @=> !(FloatingActionButton.new [
26+
],
27+
floatingActionButton @=> FloatingActionButton.new [
2828
tooltip @= "Increment",
29-
child @=> !(Icon.new Icons.add []),
29+
child @=> Icon.new Icons.add [],
3030
onPressed @= modify state (+ 1)
31-
])
31+
]
3232
]
3333

34-
app : IO Stateless
34+
app : Stateless
3535
app = Stateless.new [onBuild @= build]
3636
where
3737
build : BuildContext -> IO Widget
38-
build _ = cast <$> MaterialApp.new [
38+
build _ = widget $ MaterialApp.new [
3939
title @= appTitle,
40-
home @=> !appHome,
40+
home @=> appHome,
4141
theme @= !(ThemeData.new [
4242
-- This is the theme of your application.
4343
--
@@ -53,4 +53,4 @@ app = Stateless.new [onBuild @= build]
5353
]
5454

5555
main : IO ()
56-
main = runApp !app
56+
main = runApp app

packages/flutter/src/Flutter/FFI.idr

+23-23
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import Dart.FFI.Elab
2929
],
3030
class' "EdgeInsets" [
3131
extends "EdgeInsetsGeometry",
32-
new "all" [
32+
const $ new "all" [
3333
"value" :: "double"
3434
]
3535
],
@@ -49,7 +49,7 @@ import Dart.FFI.Elab
4949
]
5050
],
5151
class' "ButtonStyle" [
52-
new "" [
52+
const $ new "" [
5353
"backgroundColor" :? "MaterialStateProperty" :<> "Color",
5454
"foregroundColor" :? "MaterialStateProperty" :<> "Color",
5555
"textStyle" :? "MaterialStateProperty" :<> "TextStyle",
@@ -59,7 +59,7 @@ import Dart.FFI.Elab
5959
],
6060
class' "Card" [
6161
extends "Widget",
62-
new "" [
62+
const $ new "" [
6363
"key" :? "Key",
6464
"color" :? "Color",
6565
"shadowColor" :? "Color",
@@ -74,7 +74,7 @@ import Dart.FFI.Elab
7474
],
7575
class' "CircularProgressIndicator" [
7676
extends "Widget",
77-
new "" [
77+
const $ new "" [
7878
"key" :? "Key"
7979
]
8080
],
@@ -93,7 +93,7 @@ import Dart.FFI.Elab
9393
],
9494
class' "FloatingActionButton" [
9595
extends "Widget",
96-
new "" [
96+
const $ new "" [
9797
"onPressed" :? "IO" :<> "()",
9898
"tooltip" :? "String",
9999
"child" :? "Widget"
@@ -115,22 +115,22 @@ import Dart.FFI.Elab
115115
],
116116
class' "IconButton" [
117117
extends "Widget",
118-
new "" [
118+
const $ new "" [
119119
"onPressed" :? "IO" :<> "()",
120120
"tooltip" :? "String",
121121
"icon" :? "Widget",
122122
"alignment" :? "AlignmentGeometry"
123123
]
124124
],
125125
class' "InputDecoration" [
126-
new "" [
126+
const $ new "" [
127127
"labelText" :? "String",
128128
"hintText" :? "String"
129129
]
130130
],
131131
class' "LinearProgressIndicator" [
132132
extends "Widget",
133-
new "" [
133+
const $ new "" [
134134
"key" :? "Key",
135135
"value" :? "double",
136136
"backgroundColor" :? "Color",
@@ -142,7 +142,7 @@ import Dart.FFI.Elab
142142
],
143143
class' "MaterialApp" [
144144
extends "Widget",
145-
new "" [
145+
const $ new "" [
146146
"title" :? "String",
147147
"home" :? "Widget",
148148
"theme" :? "ThemeData",
@@ -184,7 +184,7 @@ import Dart.FFI.Elab
184184
],
185185
class' "Scaffold" [
186186
extends "Widget",
187-
new "" [
187+
const $ new "" [
188188
"appBar" :? "Widget",
189189
"body" :? "Widget",
190190
"floatingActionButton" :? "Widget",
@@ -193,7 +193,7 @@ import Dart.FFI.Elab
193193
],
194194
class' "Slider" [
195195
extends "Widget",
196-
new "" [
196+
const $ new "" [
197197
"value" :? "Double",
198198
"onChanged" :? "Double" :-> "IO" :<> "()",
199199
"onChangeStart" :? "Double" :-> "IO" :<> "()",
@@ -206,7 +206,7 @@ import Dart.FFI.Elab
206206
],
207207
class' "TextButton" [
208208
extends "Widget",
209-
new "" [
209+
const $ new "" [
210210
"onPressed" :? "IO" :<> "()",
211211
"style" :? "ButtonStyle",
212212
"child" :? "Widget"
@@ -250,7 +250,7 @@ import Dart.FFI.Elab
250250
],
251251
package "package:flutter/widgets.dart" [
252252
class' "Align" [
253-
new "" [
253+
const $ new "" [
254254
"key" :? "Key",
255255
"alignment" :? "AlignmentGeometry",
256256
"widthFactor" :? "double",
@@ -267,7 +267,7 @@ import Dart.FFI.Elab
267267
],
268268
class' "Center" [
269269
extends "Widget",
270-
new "" [
270+
const $ new "" [
271271
"child" :? "Widget"
272272
]
273273
],
@@ -281,7 +281,7 @@ import Dart.FFI.Elab
281281
],
282282
class' "CustomPaint" [
283283
extends "Widget",
284-
new "" [
284+
const $ new "" [
285285
"key" :? "Key",
286286
"child" :? "Widget",
287287
"painter" :? "CustomPainter",
@@ -295,13 +295,13 @@ import Dart.FFI.Elab
295295
],
296296
class' "Expanded" [
297297
extends "Widget",
298-
new "" [
298+
const $ new "" [
299299
"child" :? "Widget"
300300
]
301301
],
302302
class' "FittedBox" [
303303
extends "Widget",
304-
new "" [
304+
const $ new "" [
305305
"key" :? "Key",
306306
"child" :? "Widget",
307307
"fit" :? "BoxFit",
@@ -311,7 +311,7 @@ import Dart.FFI.Elab
311311
],
312312
class' "Form" [
313313
extends "Widget",
314-
new "" [
314+
const $ new "" [
315315
"child" :? "Widget",
316316
"onChanged" :? "IO" :<> "()",
317317
"autovalidateMode" :? "AutovalidateMode"
@@ -342,7 +342,7 @@ import Dart.FFI.Elab
342342
],
343343
class' "Icon" [
344344
extends "Widget",
345-
new "" [
345+
const $ new "" [
346346
"icon" :: "IconData",
347347
"key" :? "Key"
348348
]
@@ -361,15 +361,15 @@ import Dart.FFI.Elab
361361
],
362362
class' "Padding" [
363363
extends "Widget",
364-
new "" [
364+
const $ new "" [
365365
"key" :? "Key",
366366
"padding" :? "EdgeInsetsGeometry",
367367
"child" :? "Widget"
368368
]
369369
],
370370
class' "SizedBox" [
371371
extends "Widget",
372-
new "" [
372+
const $ new "" [
373373
"key" :? "Key",
374374
"width" :? "double",
375375
"height" :? "double",
@@ -378,7 +378,7 @@ import Dart.FFI.Elab
378378
],
379379
class' "Text" [
380380
extends "Widget",
381-
new "" [
381+
const $ new "" [
382382
"text" :: "String",
383383
"textScaleFactor" :? "Double",
384384
"style" :? "TextStyle",
@@ -414,7 +414,7 @@ import Dart.FFI.Elab
414414
class' "Color" [
415415
],
416416
class' "Offset" [
417-
new "" ["dx" :: "Double", "dy" :: "Double"],
417+
const $ new "" ["dx" :: "Double", "dy" :: "Double"],
418418
final "Double" "dx",
419419
final "Double" "dy"
420420
],

packages/flutter/src/Flutter/Widgets/Functions.idr

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ export
1010
runApp : {widget : Type} -> IsAssignableFrom Widget widget => widget -> IO ()
1111
runApp w = primIO (prim__dart_invoke "runApp,package:flutter/widgets.dart" [] [w] none)
1212

13-
||| A better alternative to `cast <$> widgetConstructor`.
13+
||| A simpler alternative to `pure (cast widget)`.
1414
%inline
1515
export
16-
widget : {a : Type} -> Cast a Widget => IO a -> IO Widget
17-
widget ctor = pure (cast !ctor)
16+
widget : {a : Type} -> Cast a Widget => a -> IO Widget
17+
widget w = pure (cast w)
1818

1919
%inline
2020
export

packages/flutter/src/Flutter/Widgets/Painter.idr

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class _Painter extends material.CustomPainter {
1717
1818
final void Function(material.Canvas, material.Size) onPaint;
1919
20-
_Painter({this.onPaint});
20+
const _Painter({@material.required this.onPaint});
2121
2222
@$.override
2323
void paint(material.Canvas canvas, material.Size size) {
@@ -36,7 +36,7 @@ class _Painter extends material.CustomPainter {
3636
partial' $
3737
class' "Painter" [
3838
extends "CustomPainter",
39-
new "" [
39+
const $ new "" [
4040
"onPaint" :? "Canvas" :-> "Size" :-> "IO" :<> "()"
4141
]
4242
]

packages/flutter/src/Flutter/Widgets/Stateful.idr

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class _Stateful extends material.StatefulWidget {
1414
final $.Object initialState;
1515
final material.Widget Function(_StatefulWidgetState, material.BuildContext) onBuild;
1616
17-
_Stateful({material.Key key, this.initialState, this.onBuild}) : super(key: key);
17+
const _Stateful({material.Key key, this.initialState, this.onBuild}) : super(key: key);
1818
1919
@$.override
2020
_StatefulState createState() => _StatefulState(initialState, onBuild);
@@ -96,8 +96,8 @@ namespace Stateful
9696

9797
%inline
9898
public export
99-
new : {stateType : Type} -> Stateful.New.NamedParameters {stateType = stateType} -> IO Stateful
100-
new ps = primIO (prim__dart_new Stateful "" [] ps)
99+
new : {stateType : Type} -> Stateful.New.NamedParameters {stateType = stateType} -> Stateful
100+
new ps = prim__dart_new_const Stateful "" [] ps
101101

102102
%inline
103103
public export

packages/flutter/src/Flutter/Widgets/Stateless.idr

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import 'package:flutter/material.dart' as material;
1414
1515
class _Stateless extends material.StatelessWidget {
1616
final material.Widget Function(material.BuildContext) onBuild;
17-
_Stateless({material.Key key, this.onBuild}) : super(key: key);
17+
const _Stateless({material.Key key, this.onBuild}) : super(key: key);
1818
@$.override
1919
material.Widget build(material.BuildContext context) {
2020
return onBuild(context);
@@ -27,7 +27,7 @@ class _Stateless extends material.StatelessWidget {
2727
partial' $
2828
class' "Stateless" [
2929
extends "Widget",
30-
new "" [
30+
const $ new "" [
3131
"key" :? "Key",
3232
"onBuild" :? "BuildContext" :-> "IO" :<> "Widget"
3333
]

0 commit comments

Comments
 (0)