Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toIntOption (and Long, Boolean, et al) #566

Merged
merged 2 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ All future versions will remain backwards binary compatible with 2.0.0. (The 1.0

## How it works

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.

The 2.11 and 2.12 versions have the needed compatibility code in this package.

### Changed methods

The 2.13 collections redesign did not break source compatibility for most ordinary code, but there are some exceptions.

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

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.

The 2.11 and 2.12 versions have the needed compatibility code in this package.
### New collections

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.)

And it adds backported versions of some 2.13 collections methods such as `maxOption`.
### New collection methods

Support is included for some 2.13 collections methods such as `maxOption`.

### Other new classes

Support is included for some non-collections classes, such as:

* `@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.)

### Other new methods

Support is included for some other methods, such as:

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

## Migration rules

Expand All @@ -68,7 +84,7 @@ scalacOptions += "-P:semanticdb:synthetics:on"
```bash
// sbt shell
> scalafixEnable
> scalafixAll dependency:[email protected]:scala-collection-migrations:<version>
> scalafixAll dependency:[email protected]:scala-collection-migrations:<version>
```

### Collection213CrossCompat
Expand All @@ -87,7 +103,7 @@ scalacOptions += "-P:semanticdb:synthetics:on"
```bash
// sbt shell
> scalafixEnable
> scalafixAll dependency:[email protected]:scala-collection-migrations:<version>
> scalafixAll dependency:[email protected]:scala-collection-migrations:<version>
```

### Fixing unused import warnings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,60 @@ private[compat] trait PackageShared {
def newBuilder: m.Builder[A, C] = factory()
}

implicit class StringOps(s: String) {

/**
* Try to parse as a `Boolean`
* @return `Some(true)` if the string is "true" case insensitive,
* `Some(false)` if the string is "false" case insensitive,
* and `None` if the string is anything else
* @throws java.lang.NullPointerException if the string is `null`
*/
def toBooleanOption: Option[Boolean] = StringParsers.parseBool(s)

/**
* Try to parse as a `Byte`
* @return `Some(value)` if the string contains a valid byte value, otherwise `None`
* @throws java.lang.NullPointerException if the string is `null`
*/
def toByteOption: Option[Byte] = StringParsers.parseByte(s)

/**
* Try to parse as a `Short`
* @return `Some(value)` if the string contains a valid short value, otherwise `None`
* @throws java.lang.NullPointerException if the string is `null`
*/
def toShortOption: Option[Short] = StringParsers.parseShort(s)

/**
* Try to parse as an `Int`
* @return `Some(value)` if the string contains a valid Int value, otherwise `None`
* @throws java.lang.NullPointerException if the string is `null`
*/
def toIntOption: Option[Int] = StringParsers.parseInt(s)

/**
* Try to parse as a `Long`
* @return `Some(value)` if the string contains a valid long value, otherwise `None`
* @throws java.lang.NullPointerException if the string is `null`
*/
def toLongOption: Option[Long] = StringParsers.parseLong(s)

/**
* Try to parse as a `Float`
* @return `Some(value)` if the string is a parsable `Float`, `None` otherwise
* @throws java.lang.NullPointerException If the string is null
*/
def toFloatOption: Option[Float] = StringParsers.parseFloat(s)

/**
* Try to parse as a `Double`
* @return `Some(value)` if the string is a parsable `Double`, `None` otherwise
* @throws java.lang.NullPointerException If the string is null
*/
def toDoubleOption: Option[Double] = StringParsers.parseDouble(s)
}

implicit def genericCompanionToCBF[A, CC[X] <: GenTraversable[X]](
fact: GenericCompanion[CC]): CanBuildFrom[Any, A, CC[A]] = {
/* see https://github.com/scala/scala-collection-compat/issues/337
Expand Down
Loading