rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

Include more JS primitives for Untagged variants

Open nojaf opened this issue 1 year ago • 2 comments

I'm trying to model blobParts of https://developer.mozilla.org/en-US/docs/Web/API/Blob/Blob#blobparts for the new WebAPI bindings.

@unboxed
type blobPart =
  | String(string)
  | Blob(blob)
  | ArrayBuffer(ArrayBuffer.t)
  | TypedArray(TypedArray.t<int>)

However, I cannot use ArrayBuffer.t and TypedArray.t in combination with the @unboxed. Could some of these be extended?

nojaf avatar Nov 05 '24 07:11 nojaf

I'm hitting the limitations of @unboxed again for https://github.com/rescript-lang/experimental-rescript-webapi/pull/7/files#r1839785692

Modelling https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fillStyle

It should be something like

@unboxed
type fillStyle =
  | String(string)
  | Gradient(canvasGradient)
  | Pattern(canvasPattern)

It would be great if I can input some sort of check for the unboxed via attribute, something like:

| @check("$0 instanceof CanvasGradient") Gradient(canvasGradient) // where $0 is the object in place.

nojaf avatar Nov 13 '24 09:11 nojaf

Blob is a part of Web API (File API spec), not a JS primitive.

It is actually an outer class and has more complex semantics at the prototype level than JS primitives. Handling this safely will add tons of complexity to the compiler.

As we discussed before (https://github.com/rescript-lang/rescript-core/issues/240), I think it's not a good long-term direction for the compiler to add specializations for specific environments, because it bloats the compiler code, reduces configurability, and limits future enhancements.

If it cannot be extended in a generalized way, it should be kept to a minimum as possible.

cometkim avatar Mar 18 '25 22:03 cometkim