Skip to content

Instantly share code, notes, and snippets.

@dupuchba
Created February 24, 2022 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dupuchba/e9202a768248a61d5a4a177935d6f4bb to your computer and use it in GitHub Desktop.
Save dupuchba/e9202a768248a61d5a4a177935d6f4bb to your computer and use it in GitHub Desktop.
ClojureDart Magicasting
;; Example 1: inlined dart List
(w/Column. :children #dart [(w/Text. "aa")])
;; Example 2: not inlined dart list
(let [t #dart [(w/Text. "aa")]]
(w/Column. :children t))
;; Example 3: PV with inlined widget
(w/Column. :children [(w/Text. "aa")])
;; Example 4: `vec-of-widgets` `dynamic` (it can be anything)
(fn [vec-of-widgets]
(w/Column. :children vec-of-widgets))
;; Example 5: `vec-of-widgets` typed as PersistentVector<Widget>
(fn [^#/(PersistentVector w/Widget) vec-of-widgets]
(w/Column. :children vec-of-widgets))
/// Example 1: inlined dart list
/// In this case we do nothing, inlined List can be infered by Dart compiler
return f_widgets.Column(children: [f_widgets.Text("aa", ), ], );
/// Example 2: not inlined dart list
/// Dart list is not inlined and can't be infered as List<Widget>, we simply create a new variable and cast it to Widget
final dc.List<dc.dynamic> t$1=[f_widgets.Text("aa", ), ];
final dc.List<dc.dynamic> castmethod$1=t$1;
final dc.List<f_widgets.Widget> wrapper_f$1=(castmethod$1.cast<f_widgets.Widget>());
return f_widgets.Column(children: wrapper_f$1, );
/// Example 3: PV with inlined widget
/// PersistentVector are Dart List, their type-parameters is dynamic by default, we cast it to PersistentVector<Widget>
/// and pass it to the widget
final dc.List<dc.dynamic> fl$1=(dc.List.filled(1, f_widgets.Text("aa", ), ));
final lcoc_core.PersistentVector castmethod$1=lcoc_core.$_vec_owning(fl$1, );
final dc.List<f_widgets.Widget> wrapper_f$1=(castmethod$1.cast<f_widgets.Widget>());
return f_widgets.Column(children: wrapper_f$1, );
/// Example 4: `vec-of-widgets` `dynamic` (it can be anything)
/// In this case there is no way to infer `vec-of-widgets`, user did not provide type hints so it can be anything.
/// First we test if `vec-of`widgets` is a List<Widget>, if it is we assigned its value to the wrapper variable,
/// if not we cast it as a List<Widget>
return (
dc.dynamic vec_of_widgets$1,
) {
final dc.dynamic castmethod$1 = vec_of_widgets$1;
late final dc.List<f_widgets.Widget> wrapper_f$1;
if ((castmethod$1 is dc.List<f_widgets.Widget>)) {
wrapper_f$1 = castmethod$1;
} else {
wrapper_f$1 = ((castmethod$1 as dc.List).cast<f_widgets.Widget>());
}
return f_widgets.Column(
children: wrapper_f$1,
);
};
/// Example 5: `vec-of-widgets` typed as PersistentVector<Widget>
/// We do almost nothing except promoting `vec-of-widgets` type to PersistentVector<Widget>
return (
dc.dynamic vec_of_widget$1,
) {
return f_widgets.Column(
children:
(vec_of_widget$1 as lcoc_core.PersistentVector<f_widgets.Widget>),
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment