You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This typed interface might catch some errors for you, but it won't stop you from accidentally calling your function on a person's name (`locateAddress(person.name)`). Or maybe more likely, since the results are both `Double`s, it would be pretty easy to get them mixed up (`val (latitude, longitude) = locateAddress(address)`).
19
19
20
20
With tagged types, the function would take the same values, but its signature might look like:
Then, declare your tagged type, specifying the underlying type as a string literal:
59
59
60
-
```
60
+
```scala
61
61
@tagged typeAddress=String
62
62
@tagged typeLongitude=Double
63
63
@tagged typeLatitude=Double
64
64
```
65
65
66
66
When data enters your system as a string, upgrade it to the tagged type:
67
67
68
-
```
68
+
```scala
69
69
valaddress=Address.fromString("123 Main Street")
70
70
vallongitude=Longitude.fromDouble(44.12345)
71
71
```
72
72
73
73
In a lot of cases, the compiler will let you pass a tagged type where its underlying type is expected, but not always. When you need to widen back to the underlying type:
- You can often write generic implicits for [de]serialization of tagged types. For example, to do this with Spray-JSON serialization, you might do something like:
0 commit comments