How to get make a random color hex generator in rescript?

Sorry for a silly question but how can I make a random color hex generator in rescript ?
I’ve tried many methods but I’ve failed to get a good grasp of how rescript really work so far.
I could really need help with the task or at least a lead of how I can implement it correctly in rescript.

This is one of the examples of how I’ve failed (this is by far the closest that I got to get the module working):

let validHexCharacterArray = [“0”, “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “A”, “B”, “C”, “D”, “E”, “F”]

module ColorHex = {
let getRandomHexColorString = () => {

let hexColorString = "#";

for  i in 0 to 6 {
  let shuffleString = Js.String.concatMany(Belt.Array.shuffle(validHexCharacterArray))
  hexColorString :=   hexColorString+ charAt(validHexCharacters, 0)
  /* This has type: string
     Somewhere wanted: ref<'a> */
}
 hexColorString

}
}

https://rescript-lang.org/try?code=DYUwLgBATghgdgEwPYFsDCThKhAvAKAggCkBnAOgFkYwALcgMy2wAoyqb7ZFUWBKCACpyEAIwA2AOzSATKICs5PgFoAfOwCScMOTBIAymCgBLOAHMA6sboAlGAmMAPFgD9YDx7gl98QA
Logic source: How To Generate a Random Color in JavaScript | CSS-Tricks - CSS-Tricks

2 Likes

To explain the exact error you posted, let bindings are immutable, so you can’t change the value of hexColorString.

The := is a special operator for mutating a ref value. Since hexColorString is a string, not a ref, then := doesn’t work. (Thus the type error.)

What you probably meant to write was this:

let hexColorString = ref("#")

A ref is simply a record with a mutable field called contents. Writing x := y is basically syntax sugar for x.contents = y.

1 Like