mlhaufe / brevity

Brevity is a library that enables Feature-Oriented Programming (FOP) and solves the expression problem in a manner that makes data and operation declarations trivial to define and compose.
GNU Affero General Public License v3.0
1 stars 0 forks source link

Investigate Data property guards #46

Closed mlhaufe closed 1 year ago

mlhaufe commented 1 year ago

Converting to TypeScript (#28) seems to be more trouble than it's worth due to the non-trivial types required (nearly as large as the implementation itself) and the limitations of TypeScript to conveniently express what is going on.

I think it's worth considering property guards:

const Shape = Data({ 
  Circle: { radius: { guard: Number },
  Rectangle: { width: { guard: Number }, height: { guard: Number } }
})

const Point2 = Data({ x: { guard: Number }, y: { guard: Number } })

const Disk = Data({
  position: { guard: Point2 }, 
  velocity: { guard: Point2 },
  radius: { guard: Number },
  item: {}
])

This may have the added benefit of avoiding the ugliness required currently in #28. Even if that issue was resolved, a data declaration like Shape would not be interpreted properly anyway as an enum / subtype.

This opens up some other possibilities as well such as derived properties.

A guard can be: a Constructor, Data declaration, Variant Declaration, a predicate function of length 1.

If it's a function then the long-hand form must be used.

mlhaufe commented 1 year ago

Suspending work to v0.11.0 due to #53