-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve type-safety with data
#5
Comments
Yes, your understanding is correct that that is the safe idiom that most D3.js code follows, with two exceptions. First, an selection
|. data (fun d i -> ... )
|- nest (enter <.> append "element") [...]
|- nest update [ ... ]
|- nest exit [ ... ] Second, reordering of the various cases does sometimes occur when doing animations, or transitions in D3 parlance. Your proposal is very close to what's required in order to make data binds type-safe, but not quite. Perhaps a clarification of type-safety in this context is necessary. First, note that data binds mutate the data bound to existing elements. Also, remember that sequencing cannot track changes to types of data involved in this mutation. Hence: (selectAll "things" : (_, int) D3.t)
|- data (fun _ _ _ -> ["one"; "two"; "three"])
|- attr (fun _ d _ -> string_of_int d) (* val d : string, actually *) ... will type check, but is not type-safe. Second, the enter <.> str attr "class" "whoops" ... will type check, but will produce a runtime error. The former is more what I think of as unsoundness, (e.g., absence of the value restriction) while the latter is more of an API property that isn't expressed in types (e.g., In order to avoid the unsoundness, it's sufficient to introduce the following operator: val safe_data : string -> ('a -> int -> 'b list) -> ('a, 'b) t This simply eliminates the possibility of involving the initial To avoid the runtime error from misuses of val safe_enter : string -> ('a, 'a) t I was considering adding the open D3
include Safe_bind (* : sig
val bind : string -> ('a -> int -> 'b list) -> ('a, 'b) t
*) I suppose a similar thing could be done with |
Oh, I got bit by that one later on, yeah. The |
After having played a bit with
d3
.. It's really easy to get exceptions by usingdata
not exactly the way it's supposed to be used.My understanding is that the safe idiom is :
with
selection
something obtained fromselect
orselectAll
(notrun
).Is it right ? If that's the case, why not just hardcode it ?
I propose adding the type
selection
returned by selecting functions and:The text was updated successfully, but these errors were encountered: