Skip to content

Commit 0737983

Browse files
authored
Merge pull request #566 from rjolly/main
2 parents 7030af3 + 81d0d33 commit 0737983

File tree

7 files changed

+782
-38
lines changed

7 files changed

+782
-38
lines changed

README.md

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ All future versions will remain backwards binary compatible with 2.0.0. (The 1.0
2222

2323
## How it works
2424

25+
The 2.13 and 3.0 versions consist only of an empty `scala.collection.compat` package object, so `import scala.collection.compat._` won't cause an error in cross-compiled code.
26+
27+
The 2.11 and 2.12 versions have the needed compatibility code in this package.
28+
29+
### Changed methods
30+
2531
The 2.13 collections redesign did not break source compatibility for most ordinary code, but there are some exceptions.
2632

2733
For example, the `to` method is used with a type parameter in 2.12:
@@ -37,15 +43,25 @@ import scala.collection.compat._
3743
xs.to(List)
3844
```
3945

40-
The 2.13 and 3.0 versions consist only of an empty `scala.collection.compat` package object, so `import scala.collection.compat._` won't cause an error in cross-compiled code.
41-
42-
The 2.11 and 2.12 versions have the needed compatibility code in this package.
46+
### New collections
4347

4448
The library also adds backported versions of new collection types, such as `immutable.ArraySeq` and `immutable.LazyList`. (On 2.13, these types are just aliases to standard library types.)
4549

46-
And it adds backported versions of some 2.13 collections methods such as `maxOption`.
50+
### New collection methods
51+
52+
Support is included for some 2.13 collections methods such as `maxOption`.
53+
54+
### Other new classes
55+
56+
Support is included for some non-collections classes, such as:
57+
58+
* `@nowarn` annotation, added in 2.13.2 and 2.12.13. (The 2.11 `@nowarn` doesn't do anything, but its presence facilitates cross-compilation.)
59+
60+
### Other new methods
61+
62+
Support is included for some other methods, such as:
4763

48-
And, it includes support for some non-collections classes such as the `@nowarn` annotation added in 2.13.2.
64+
* `toIntOption` (and `Long`, et al) on `String`
4965

5066
## Migration rules
5167

@@ -68,7 +84,7 @@ scalacOptions += "-P:semanticdb:synthetics:on"
6884
```bash
6985
// sbt shell
7086
> scalafixEnable
71-
> scalafixAll dependency:[email protected]:scala-collection-migrations:<version>
87+
> scalafixAll dependency:[email protected]:scala-collection-migrations:<version>
7288
```
7389

7490
### Collection213CrossCompat
@@ -87,7 +103,7 @@ scalacOptions += "-P:semanticdb:synthetics:on"
87103
```bash
88104
// sbt shell
89105
> scalafixEnable
90-
> scalafixAll dependency:[email protected]:scala-collection-migrations:<version>
106+
> scalafixAll dependency:[email protected]:scala-collection-migrations:<version>
91107
```
92108

93109
### Fixing unused import warnings

compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,60 @@ private[compat] trait PackageShared {
5353
def newBuilder: m.Builder[A, C] = factory()
5454
}
5555

56+
implicit class StringOps(s: String) {
57+
58+
/**
59+
* Try to parse as a `Boolean`
60+
* @return `Some(true)` if the string is "true" case insensitive,
61+
* `Some(false)` if the string is "false" case insensitive,
62+
* and `None` if the string is anything else
63+
* @throws java.lang.NullPointerException if the string is `null`
64+
*/
65+
def toBooleanOption: Option[Boolean] = StringParsers.parseBool(s)
66+
67+
/**
68+
* Try to parse as a `Byte`
69+
* @return `Some(value)` if the string contains a valid byte value, otherwise `None`
70+
* @throws java.lang.NullPointerException if the string is `null`
71+
*/
72+
def toByteOption: Option[Byte] = StringParsers.parseByte(s)
73+
74+
/**
75+
* Try to parse as a `Short`
76+
* @return `Some(value)` if the string contains a valid short value, otherwise `None`
77+
* @throws java.lang.NullPointerException if the string is `null`
78+
*/
79+
def toShortOption: Option[Short] = StringParsers.parseShort(s)
80+
81+
/**
82+
* Try to parse as an `Int`
83+
* @return `Some(value)` if the string contains a valid Int value, otherwise `None`
84+
* @throws java.lang.NullPointerException if the string is `null`
85+
*/
86+
def toIntOption: Option[Int] = StringParsers.parseInt(s)
87+
88+
/**
89+
* Try to parse as a `Long`
90+
* @return `Some(value)` if the string contains a valid long value, otherwise `None`
91+
* @throws java.lang.NullPointerException if the string is `null`
92+
*/
93+
def toLongOption: Option[Long] = StringParsers.parseLong(s)
94+
95+
/**
96+
* Try to parse as a `Float`
97+
* @return `Some(value)` if the string is a parsable `Float`, `None` otherwise
98+
* @throws java.lang.NullPointerException If the string is null
99+
*/
100+
def toFloatOption: Option[Float] = StringParsers.parseFloat(s)
101+
102+
/**
103+
* Try to parse as a `Double`
104+
* @return `Some(value)` if the string is a parsable `Double`, `None` otherwise
105+
* @throws java.lang.NullPointerException If the string is null
106+
*/
107+
def toDoubleOption: Option[Double] = StringParsers.parseDouble(s)
108+
}
109+
56110
implicit def genericCompanionToCBF[A, CC[X] <: GenTraversable[X]](
57111
fact: GenericCompanion[CC]): CanBuildFrom[Any, A, CC[A]] = {
58112
/* see https://github.com/scala/scala-collection-compat/issues/337

0 commit comments

Comments
 (0)