Skip to content

Commit 102f271

Browse files
committed
Add better docs to pretty print
1 parent 411dd70 commit 102f271

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

docs/src/main/tut/pretty_print.md

+20-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ section: "main_menu"
55
position: 3
66
---
77

8-
## Case class Example
8+
## Typesafe pretty-print of case-class.
99

1010
```scala
1111

@@ -32,39 +32,44 @@ This works for any level of **deep nested structure of case class**. This is don
3232
The main idea here is, if any field in any part of the nested case class isn't safe to be converted to string, it will throw a compile time.
3333
Also, if any part of case classes has `Secret`s in it, the value will be hidden. More on this in `Secret / Password` section
3434

35-
## Type-safety
35+
## Why case-classes ?
3636

37-
As mentioned in the simple example, `safeStr` can take **_ONLY_** **strings** and **case class instances**.
38-
Infact, the purpose of `safe-string-interpolation` is to make sure you are passing only Strings to `safeStr`.
39-
Do remember that, if it isn't a case class passed to `safeStr`, there will not be any automatic conversion of non-string types to string types through any automatic `Safe` instances in scope.
40-
This is intentional. You have to explicitly convert your non-case-class types to string using `nonString.asStr`.
41-
42-
#### Case class isn't a string ! So why should it work with safeStr ?
43-
Delegating the job of stringifying a case class to the user has always been troublesome and it kills the user's time. In fact, it is a popular problem that we solved here.
37+
While the purpose of `safe-string-interpolation` is to make sure you are passing only Strings to `safeStr`, it works for case-class instances as well.
38+
There is a reason for this.
39+
40+
Delegating the job of stringifying a case class to the user has always been an infamous problem and it kills the user's time.
4441
The `safe-string-interpolation` takes up this tedious job, and macros under the hood converts it to a readable string, while hiding `Secret` types.
45-
As mentioned earlier, anything else other than strings and (automatically stringifiable) case classes will be rejected by compiler.
4642

4743
```scala
4844

45+
@ import com.thaj.safe.string.interpolator.SafeString._
46+
import com.thaj.safe.string.interpolator.SafeString._
47+
4948
@ case class Test(list: List[String])
5049
defined class Test
5150

5251
@ val test = Test(List("foo", "bar"))
5352
test: Test = Test(List("foo", "bar"))
5453

5554
@ safeStr"test will work $test"
56-
res14: com.thaj.safe.string.interpolator.SafeString = SafeString("test will work { list: foo,bar }")
55+
res4: com.thaj.safe.string.interpolator.SafeString = SafeString("test will work { list: foo,bar }")
5756

5857
@ val test = List("foo", "bar")
5958
test: List[String] = List("foo", "bar")
6059

6160
@ safeStr"test will not work $test"
62-
cmd16.sc:1: The provided type isn't a string nor it's a case class, or you might have tried a `toString` on non-strings !
63-
val res16 = safeStr"test will not work $test"
64-
^
61+
cmd6.sc:1: The provided type isn't a string nor it's a case class, or you might have tried a `toString` on non-strings !
62+
val res6 = safeStr"test will not work $test"
63+
^
64+
Compilation Failed
65+
66+
@ safeStr"test will work by telling the compiler, yes, it is a string ${test.toString}"
67+
cmd6.sc:1: Identified `toString` being called on the types. Either remove it or use <yourType>.asStr if it has an instance of Safe.
68+
val res6 = safeStr"test will work by telling the compiler, yes, it is a string ${test.toString}"
69+
^
6570
Compilation Failed
6671

6772
@ safeStr"test will work by telling the compiler, yes, it is a string ${test.asStr}"
68-
res16: com.thaj.safe.string.interpolator.SafeString = SafeString("test will work by telling the compiler, yes, it is a string foo,bar")
73+
res6: com.thaj.safe.string.interpolator.SafeString = SafeString("test will work by telling the compiler, yes, it is a string foo,bar")
6974

7075
```

0 commit comments

Comments
 (0)