Is there any way to ignore warnings for a single line?

I’m trying to write a deep clone function like so:

let deepClone = (obj: 'a): 'a => %raw(`
  JSON.parse(JSON.stringify(obj))
`); 

let line1 = {"p0": {"x": 5, "y": 10}, "p1": {"x": 20, "y": 30}}
let line2 = deepClone(line1)

The playground gives me the following warning:

[W] Line 1, column 17:
unused variable obj.

The docs about @@warning say that it modifies the enabled warnings for the entire module. Is there any way this could be restricted to a single line?

1 Like

why not just

let deepClone = (_obj: 'a): 'a => %raw(`
  JSON.parse(JSON.stringify(_obj))
`); 

?

1 Like

@flo-pereira’s solution is probably the better one, but generally you can deactivate the warning for as many lines as you want and then activate it again:

@@warning("-27")

let deepClone = (obj: 'a): 'a => %raw(`
  JSON.parse(JSON.stringify(obj))
`); 

@@warning("+27")
2 Likes

you can also just define the whole function in JS:

let deepClone = %raw(`function(obj){
  return JSON.parse(JSON.stringify(obj));
}`); 
1 Like

Thanks for all of the suggestions. I’m going to go with the _obj solution since it avoids having to use @@warning. The %raw(`function(obj){...}`) solution has the unfortunate side-effect that return values are typed as 'a instead of preserve the type of the argument.

1 Like

Alternatively you could use Obj.magic in the middle (to bypass the option return type on stringifyAny) and avoid %raw completely :man_shrugging:

https://rescript-lang.org/try?code=DYUwLgBAJiIA4GFgHsB2IIF4IEMsD5cBafAKQGcA6CtS8sAJwEtUBzJgMwE8BBVLkgHkARgCtKAWxzsAxiQrVytODgbkQAUQAeqANwAofaEjAWIAIxYIAbwBEcAAy2AXDdtaXEAKwAaCLa5PcwcAXz97c087D1cAJgdwwNcAZlCQo3AIU3RYqxh4JDQQAApsiwBKIA

There’s also a->Js.Json.serializeExn->Js.Json.parseExn which avoids magic completely but it’s not a zero cost binding, it calls a wrapper function.