[Ur] Ur/Web -> JavaScript objects

Adam Chlipala adamc at csail.mit.edu
Tue Oct 8 16:57:05 EDT 2019


On 10/6/19 5:45 PM, Mark Clements wrote:
> In defining an FFI to a JavaScript library such as Chart.js, config 
> options may be defined in terms of (nested) JavaScript objects with a 
> range of potential keys/fields. Analogous to the xml tags, is there 
> any way to define a record type in Ur/Web that potentially includes a 
> subset of fields?

In my own FFI wrapping so far, I have just given fields [option] types 
when they are optional, dealing with writing in [None] values for unused 
fields manually.  I don't think it's so bad!

Since I never miss an opportunity to show off metaprogramming in Ur/Web, 
here's a function you can use to cast a record to a wider type where all 
the new fields have [option] types.

fun expand [original ::: {Type}] [additional ::: {Type}] [original ~ 
additional]
            (fl : folder additional) (r : $original)
     : $(original ++ map option additional) =
     r ++ @map0 [option] (fn [t ::_] => None) fl

fun foo (x : {A : int, B : option string, C : float, D : option bool, E 
: string}) =
     42

val example = foo (expand {A = 3, C = 1.234, D = Some True, E = "hi"})




More information about the Ur mailing list