@as applied to type definitions

Hi everyone,

I’ve been putting some types definitions around the WebGL API and I’ve hit a snag with @as and @int.

Functions such as gl.disable take an int parameter selected from a number of object constants. I’ve been able to get this working with a code such as this:

send external disable: (t, @int [
| @as(3042) #Blend
| @as(2884) #CullFace
| @as(2929) #DepthTest
| @as(3024) #Dither
| @as(32823) #PolygonOffsetFill
| @as(32926) #SampleAlphaToCoverage
| @as(32928) #SampleCoverage
| @as(3089) #ScissorTest
| @as(2960) #StencilTest
]) => unit = "disable"

However, the values in this enum are used multiple times. Ideally I would define this enum as a type and then reference the type multiple times:

type capability = @int [
| @as(3042) #Blend
| @as(2884) #CullFace
| @as(2929) #DepthTest
| @as(3024) #Dither
| @as(32823) #PolygonOffsetFill
| @as(32926) #SampleAlphaToCoverage
| @as(32928) #SampleCoverage
| @as(3089) #ScissorTest
| @as(2960) #StencilTest
]

@send external disable: (t, capability) => unit = "disable"
@send external enable: (t, capability) => unit = "enable"

But this doesn’t work. Am I doing something wrong? Is this an oversight in the current implementation? Or is this use-case not really planned for? Is there an alternative way to approach this?

Thanks,
Sam

1 Like

Hi,
@as in the externals are not first class attributes which means you have to inline the definitions here.

Note you can define it directly as below :

type capability = [
 | #3042
 | #2884
 | ..
]
1 Like

Another option that might work for you is to define the values as constants. Something like this:

module Capability = {
  type t
  external make: int => t = "%identity"

  let blend = make(3042)
  let cullFace = make(2884)
  let depthTest = make(2929)
}

type t

@send external disable: (t, Capability.t) => unit = "disable"
@send external enable: (t, Capability.t) => unit = "enable"
2 Likes

Thanks, that gives me some ideas!