alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (MayErr.scala)

This example Play Framework source code file (MayErr.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

a, aa, b, boolean, e, ee, either, mayerr, option, play, play framework, use

The MayErr.scala Play Framework example source code

/*
 * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
 */
package anorm

case class MayErr[+E, +A](toEither: Either[E, A]) {

  def flatMap[B, EE >: E](f: A => MayErr[EE, B]): MayErr[EE, B] =
    MayErr(toEither.right.flatMap(a => f(a).toEither))

  def map[B](f: A => B): MayErr[E, B] = MayErr(toEither.right.map(f))

  @deprecated(
    message = "Use `filter` on `toEither.right`.", since = "2.3.0")
  def filter[EE >: E](p: A => Boolean, error: EE): MayErr[EE, A] =
    MayErr(toEither.right.filter(p).getOrElse(Left(error)))

  @deprecated(since = "2.3.0")
  def toOptionLoggingError(): Option[A] =
    toEither.left.map(m => { println(m.toString); m }).right.toOption

  /**
   * Applies `f` if this is a failure or `s` if this is a successful value.
   *
   * @param f the function to apply on failure
   * @param s the function to apply on successful `A` value
   * @return the results of applying appropriate function
   */
  def fold[B](f: E => B, s: A => B): B = toEither.fold(f, s)

  def get = toEither.fold(e =>
    throw new RuntimeException(toEither.toString), a => a)

}

// TODO Remove (make it more explicit)
object MayErr {
  import scala.language.implicitConversions
  implicit def eitherToError[E, EE >: E, A, AA >: A](e: Either[E, A]): MayErr[EE, AA] = MayErr[E, A](e)
}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework MayErr.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.