Rescript syntax for decco.codec

How would you write something like this in ReScript syntax?

[@decco] type t = [@decco.codec (fancyEncoder, fancyDecoder)] fancyType;

Thanks,
Ethan

There is a CLI for such things:

git$npx rescript format -stdin .re
[@decco] type t = [@decco.codec (fancyEncoder, fancyDecoder)] fancyType;
@decco type t = @decco.codec((fancyEncoder, fancyDecoder)) fancyType

Ctrl-D to terminate the input

3 Likes

Thanks for the response! That tool will be very useful. I wonder if the issue that I am running into is specific to the type that I am working with. I have the following code:

@decco
type rectTuple = (float, float, float, float)

let rect_encode = ({x1, y1, x2, y2}) => (x1, y1, x2, y2)->rectTuple_encode
let rect_decode = json =>
  json->rectTuple_decode->Result.map(((x1, y1, x2, y2)) => {x1: x1, y1: y1, x2: x2, y2: y2})

@decco type rect = @decco.codec((rect_encode, rect_decode)) {
  x1: float,
  y1: float,
  x2: float,
  y2: float,
}

As you can see, I am trying to encode a 4-tuple using a record type instead of the default tuple or array.

I get the error:

 Syntax error!
  /Users/ethanbrooks/web/maker/Components.res:11:3-4
  
   9 β”‚ 
  10 β”‚ @decco type rect = @decco.codec((rect_encode, rect_decode)) {
  11 β”‚   x1: float,
  12 β”‚   y1: float,
  13 β”‚   x2: float,
  
  An inline record type declaration is only allowed in a variant constructor's declaration

There is a simpler way to do this. Just have decco derive the encoding for the tuple and use it for your record type. You are almost doing this right now, you just need to get rid of the @decco attributes from the type rect = ... type, and put the rect_encode and rect_decode functions after the type declaration. And that’s it, you now have the desired derivation.

1 Like

Ah simple enough. Thank youl

1 Like