Trait/Object

com.eharmony.aloha.feature

OptionMath

Related Docs: object OptionMath | package feature

Permalink

trait OptionMath extends AnyRef

Provides basic inline unary and binary mathematical operators for options of primitive types including.

- Option[Byte] - Option[Short] - Option[Int] - Option[Long] - Option[Float] - Option[Double]

Additionally, operations are provided that can take any scala.math.Numeric type as long as an implicit of that type is available at call time.

This functionality assumes that the type arguments are the same as it relies on scala.math.Numeric. When the types are the same, everything works seamlessly.

import com.eharmony.aloha.feature.OptionMath.Syntax._

scala> Option(1) + None
res0: Option[Int] = None

scala> None + Option(1)
res1: Option[Int] = None

scala> Option(1) + Option(2)
res3: Option[Int] = Some(3)

scala> Some(1L.toInt) + Option(2)
res4: Option[Int] = Some(3)

scala> Option(1) < Option(2)
res5: Option[Boolean] = Some(true)

scala> Option(1) >= Option(2)
res6: Option[Boolean] = Some(false)

scala> Option(1) < None
res7: Option[Boolean] = None

When the types for the function arguments don't line up, we get a compile time error.

scala> Some(1) + Some(1.5)
<console>:12: error: type mismatch;
found   : Double(1.5)
required: Int
             Some(1) + Some(1.5)
                            ^

Because inline operations require the use of implicit classes, inline syntax will incur an object creation overhead that using OptMathOps will not.

import com.eharmony.aloha.feature.OptionMath.Syntax._
import com.eharmony.aloha.feature.OptionMath.OptMathOps

val a = Option(1.5)
val b = Option(0.5)

val c = a / b                 // Additional object creation involved (slower, prettier)
val d = OptMathOps.div(a, b)  // Less object creation involved (faster, uglier)

assert(c == d)

Notice that the usual order of operations in the same.

import com.eharmony.aloha.feature.OptionMath.Syntax._

scala> Option(10 % 4 / 2) == Option(10) % Option(4) / Option(2)
res0: Boolean = true
Linear Supertypes
AnyRef, Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. OptionMath
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. sealed trait InlineFractionalSyntax[A] extends InlineSyntax[A, Fractional[A]]

    Permalink
    Attributes
    protected[this]
  2. sealed trait InlineIntegralSyntax[A] extends InlineSyntax[A, Integral[A]]

    Permalink
    Attributes
    protected[this]
  3. sealed trait InlineSyntax[A, NumType <: Numeric[A]] extends AnyRef

    Permalink
    Attributes
    protected[this]

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. object OptMathOps

    Permalink

    Should not use a wildcard import to include all functions in the object.

    Should not use a wildcard import to include all functions in the object. Instead, import the OptMathOps object.

  5. object Syntax

    Permalink
  6. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  7. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  8. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  9. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  10. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  11. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  12. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  13. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  14. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  15. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  16. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  17. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  18. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  19. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  20. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped