mirror of
https://github.com/jbranchaud/til
synced 2026-01-02 22:58:01 +00:00
Add String Interpolation With Integers And Sprintf as a reasonml til
This commit is contained in:
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
||||
For a steady stream of TILs from a variety of rocketeers, checkout
|
||||
[til.hashrocket.com](https://til.hashrocket.com/).
|
||||
|
||||
_611 TILs and counting..._
|
||||
_612 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -441,6 +441,7 @@ _611 TILs and counting..._
|
||||
### ReasonML
|
||||
|
||||
- [Pattern Match On Exceptions](reason/pattern-match-on-exceptions.md)
|
||||
- [String Interpolation With Integers And Sprintf](reason/string-interpolation-with-integers-and-sprintf.md)
|
||||
|
||||
### Ruby
|
||||
|
||||
|
||||
27
reason/string-interpolation-with-integers-and-sprintf.md
Normal file
27
reason/string-interpolation-with-integers-and-sprintf.md
Normal file
@@ -0,0 +1,27 @@
|
||||
# String Interpolation With Integers And Sprintf
|
||||
|
||||
ReasonML's [`Printf`](https://reasonml.github.io/api/Printf.html) module
|
||||
comes with a number of functions for formatting values of various types. The
|
||||
`sprintf` function allows for string interpolation.
|
||||
|
||||
```reason
|
||||
let red = 64;
|
||||
let green = 256;
|
||||
let blue = 128;
|
||||
let alpha = 1;
|
||||
|
||||
let color =
|
||||
Printf.sprintf("rbga(%i, %i, %i, %i)", red, green, blue, alpha);
|
||||
|
||||
Js.log(color);
|
||||
```
|
||||
|
||||
It functions the same as `fprintf` but instead of outputting the result to
|
||||
some channel, it returns a string. It enforces type checking as well -- the
|
||||
`%i` is specifically for integers, so using that with a type other than an
|
||||
integer will result in a compilation error.
|
||||
|
||||
See the [`Printf`](https://reasonml.github.io/api/Printf.html) docs for more
|
||||
details.
|
||||
|
||||
[source code](https://reasonml.github.io/en/try.html?reason=DYUwLgBATiAmEF4IDYAsBuAUKSBzGIAdohAEwCsyWOEARsAK4gkCMpAHNeBAIbAAOACx6ss2bgGMA9sClREmCBAAKUAJaEwAMwB0AZ37rNWgBQAiKLVw8TAUjUAaCPacvnagJRmnMWE-wgRE70TE58QjweYgBSejqyuCbSslBRQA)
|
||||
Reference in New Issue
Block a user