Giter Club home page Giter Club logo

Comments (6)

lloydmeta avatar lloydmeta commented on August 16, 2024

As of now, the withName method is case sensitive by design, so that there are no surprises when coming from the standard lib Enumeration and because I think it makes sense (no pun intended). Convoluted example; in a Drinks enum, coke may not be the same as Coke.

That said, I'm open to the idea of making withName optionally case insensitive, such as via an optional parameter caseInsensitive parameter that defaults to false.

from enumeratum.

adamdecaf avatar adamdecaf commented on August 16, 2024

What about an import? This is what I've used in a few projects to soften the impact of throwing an exception when trying to parse incoming data I don't control.

import scalaz.Monoid
object SafeEnumeration {
  implicit final class SafeEnumerationReads[E <: Enumeration](val enum: E) extends AnyVal {
    def safeWithName(name: String): Option[E#Value] = enum.values.find(_.toString equalsIgnoreCase name)
    def withNameOrZero(name: String)(implicit m: Monoid[E#Value]): E#Value = safeWithName(name) getOrElse m.zero
  }
}

Then it's really easy to use both methods on some enumeration.

object FacebookProfileCategory extends Enumeration {
  val `Musician/Band` = Value("Musician/Band")
  val Company = Value("Company")
  val Other = Value("Other")

  implicit val categoryMonoid = new Monoid[Value] {
    def zero = Other
    def append(x: Value, y: => Value) = ???
  }
}

from enumeratum.

lloydmeta avatar lloydmeta commented on August 16, 2024

Wow, that is pretty cool :) I like the way you used implicits and monoids to make it slick.

However, I think introducing a separate method 1. that doesn't throw and 2. allows for case-insensitive matching might be a better compromise here because then we can cache the case-insensitive map (faster) and it is easier to use for end-users.

e.g. On Enum[A]

trait Enum[A] {

  lazy val caseInsensitiveNamesToValuesMap: Map[String, A] = values map (v => v.toString.toLowerCase -> v) toMap

  def withNameOption(name: String, caseInsensitive: Boolean = false): Option[A] = {
    if (caseInsensitive)
      caseInsensitiveNamesToValuesMap get name.toLowerCase
    else
      namesToValuesMap get name
  }
}

What do you think?

from enumeratum.

adamdecaf avatar adamdecaf commented on August 16, 2024

Not bad, maybe the boolean should be two methods?

from enumeratum.

lloydmeta avatar lloydmeta commented on August 16, 2024

Done !

from enumeratum.

adamdecaf avatar adamdecaf commented on August 16, 2024

Cool thanks!
On Mar 6, 2015 12:26 AM, "Lloyd" [email protected] wrote:

Done !


Reply to this email directly or view it on GitHub
#5 (comment).

from enumeratum.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.