Tensegritics / ClojureDart

Clojure dialect for Flutter and Dart
1.44k stars 92 forks source link

`:or` with object destructuring #188

Closed cgrand closed 1 year ago

cgrand commented 1 year ago

:or is meaningless in the context of object destructuring (we have to assume the prop is there, can't reasonably test existence of property on dynamic objects) I believe we could repurpose :or to provide defaults when the object is nil. {:flds [firstName] :or {firstName "N/A"}} However, what should we do about {:flds [firstName lastName] :or {firstName "N/A"}}? throwing because we don't have a default for lastName sounds weird. So we should bind lastName to nil by default. If we follow this path it means that {:flds [firstName lastName] :or {}} will mean "defaults everything to nil". Which is ok but IMO {:flds [firstName lastName] :or nil} would makes for a better shorthand. Last: should we consider making this (nil defaulting) the default for object destructuring?

cgrand commented 1 year ago

Last: should we consider making this (nil defaulting) the default for object destructuring? Upon reflection, I think it's the right thing to do:

  • no special meaning added to :or {} or even :or nil
  • we can opt-out of the null check when inference (o explicit type hint) tells us it's not nullable.
cgrand commented 1 year ago

done in 6d5fc84e2e7fee6ba4ca53d4523a2b4da442421f