All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.specs2.text.FromString.scala Maven / Gradle / Ivy

There is a newer version: 3.7
Show newest version
package org.specs2
package text

import control.Exceptions._

/**
 * This typeclass is used to describe any instance which can be decoded from a String
 */
trait FromString[T] {
  def fromString(s: String): Option[T]
}

object FromString {

  implicit def StringFromString = new FromString[String] {
    def fromString(s: String): Option[String] = Option(s)
  }

  implicit def IntFromString = new FromString[Int] {
    def fromString(s: String): Option[Int] = tryo(s.toInt)
  }

  implicit def BooleanFromString = new FromString[Boolean] {
    def fromString(s: String): Option[Boolean] =
      if ("false".equals(s))     Some(false)
      else if ("true".equals(s)) Some(true)
      else                       None
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy