Working with Strings

The last data type we’ll take up in this introduction is strings. Multi-character strings are enclosed in double quotes, and the ++ operator concatenates strings. Consider this statement:

 let​ ​str​ = ​"door"​ ++ ​"bell"​; ​/* binds value "doorbell" to variable str */

Back in the discussion of output for the program, we noted that code such as the example below doesn’t compile properly because ++ requires both its operands to be strings. The variable total is float, and the compiler will complain bitterly:

 let​ ​total​ = ​float_of_int​(​qty​) *. price;
 
 let​ ​afterDiscount​ = ​total​ *​.​ (1.0 ​-​. discount);
 
 Js​​.log​(​"Price before discount: $"​ ++ ​total​ ++ ​"."​);
 Js​​.log​(​"Price after discount: $" ...

Get Web Development with ReasonML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.