Alternative to Elm Recording Extension

I am trying to migrate the library from Elm 0.15 to 0.16. The extension mechanism has been deleted.

My library offers physical calculations on bodies (represented as a record) and uses the record extension so that users can add labels and other metadata about bodies.

My sample code shows this use by adding a label to all bodies after they are created:

labeledBodies = map (\b -> { b | label = bodyLabel b.restitution b.inverseMass }) someBodies 

This list of tagged bodies is also transferred to the library:

 main = Signal.map scene (run labeledBodies tick) 

What works: hardcoding a meta parameter in Body as follows:

 type alias Body a = { pos: Vec2, -- reference position (center) velocity: Vec2, -- direction and speed inverseMass: Float, -- we usually use only inverse mass for calculations restitution: Float, -- bounciness factor shape: Shape, meta: a } 

But this makes the API more awkward because it forces the helper functions to take an additional parameter. Is there a more elegant way to handle this change?

+5
source share
2 answers

What if the meta field was of type Dict String String ? Then you don’t have to make any crazy variables like awkward. However, you lose the guarantee that all the records that you submit really have labels, so you need to work with Maybe String when you do Dict.get "label" r.meta .

+1
source

How to use a tagged join type?

 type BodyWithMeta = LabeledBody Body String labeledBodies = map (\b -> LabeledBody b (bodyLabel b.restitution b.inverseMass)) someBodies 
+1
source

Source: https://habr.com/ru/post/1236501/


All Articles