puffnfresh / roy

Small functional language that compiles to JavaScript.
http://roy.brianmckenna.org/
MIT License
834 stars 74 forks source link

Some common DOM manipulations require mutable fields #202

Open mfrawley opened 10 years ago

mfrawley commented 10 years ago

e.g. this won't work let show (el:DomElement) = el.style.display = 'block'

joneshf commented 10 years ago

Yeah, it's a bit tough to use mutability. You have a few options. Here's two.

Use something like underscore to update it.

let _ = require 'underscore'
let show el = _.extend el.style {display: 'block'}

Use something like fantasy-lenses to still deal with immutability.

let ol = (require 'fantasy-lenses').objectLens
let show2 el =
  (((ol 'style') .andThen(ol 'display')).run el).set 'block'