Retrive value from string type in switch statement?


#1

Hi there!

I’m new to Reason/ML… please bear with me.

I have this type:

type record = {
  id_: int,
  subject: option(string),
}

Later in a React component, I’m trying to read out the string value from record.subject:

  <li key=(string_of_int(record.id_))>
    (
      switch (record.subject) {
      | None => <div> (React.stringToElement("foo")) </div>
      | string(s) => <div> (React.stringToElement(s)) </div>
      }
    )
  </li>
 

Unfortunately this doesn’t work. I keep getting unknown syntax errors. I think there’s something wrong with string(s)… I guess it’s something really basic I’m missing here. Could someone please point me to the right direction? :slight_smile:


#2

Ouch, it just clicked a minute after I wrote the question…

For the option type it’s None and Some. This works fine:

  switch (record.subject) {
  | None => <div> (React.stringToElement("foo")) </div>
  | Some(s) => <div> (React.stringToElement(s)) </div>
  }

#3

Sometimes talking about problems magically solves them :tada:
I love it when it workls like that :sunglasses: