What is `assert` used for?

GPT suggested using | _ => assert false when using switch pattern matching on a string

example

switch text {
  | "Apple" => Apple
  | _ => assert false
}

where can I read documentation or know what it is doing?

You can use it as a todo flag, then the ide would work normally😅

It throws the Assert_failure exception. In my opinion it should be removed from ReScript.

https://rescript-lang.org/try?version=v10.1.2&code=C4TwDgpgBMULxQIJjAG2gHygeQE4EMA7AcwgCh1ZgIAPWBAUgIHcAKAAwCIAzAe187sAlGTIApAM4A6VL2KsJzAJbAAxgAsYtWAG8yUKFk7I0ETvAB8SFOn2GoAfUtR8EiRFyxu+VO7IBfISA

It’s better to use panic if you’re using ReScript Core, as that throws a real JS error which will give you a stack trace etc:

https://rescript-lang.org/try?version=v11.0.0-beta.1&code=PYBwpgdgBASmDOBjATgSxAFwMLGWAUPhgJ7hQZQC8UAgiCADZhQA+UA8sgIYQDmBTChjAAPCtQCk3AO4AKAAYAiAGbBgi+QEpCAKXgA6BsF6z8UKPGmoMiABblRFAN5nWURXUZhFVAHy16Jlc2AH0-KBAeVERZRQBVCFFwRGEAEwsMND5FbXMAXwAafG0gA

3 Likes

super, I’ll use this instead, thanks

Why RescriptCore chose panic? assert also has a stack trace. It also shows the file and location in the .res file.

{
  RE_EXN_ID: 'Assert_failure',
  _1: [ 'assert.res', 6, 9 ],
  Error: Error
      at file:///home/pedro/Desktop/learning-rescript/src/assert.bs.mjs:18:16
      at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
      at async Promise.all (index 0)
      at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
      at async loadESM (node:internal/process/esm_loader:91:5)
      at async handleMainPromise (node:internal/modules/run_main:65:12)
}