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

org.scalactic.NormMethods.scala Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2001-2013 Artima, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.scalactic

/**
 * Provides an implicit conversion that allows norm to be invoked on any value of type
 * T for which an implicit Normalization[T] exists.
 *
 * 

* Here's an example: *

* *
 * scala> import org.scalactic._
 * import org.scalactic._
 *
 * scala> import StringNormalizations._
 * import StringNormalizations._
 *
 * scala> implicit val stringNormalization = lowerCased and trimmed
 * stringNormalization: org.scalactic.Uniformity[String] = org.scalactic.Uniformity$$anon$1@19ba67ec
 *
 * scala> import NormMethods._
 * import NormMethods._
 *
 * scala> val s = " There "
 * s: String = " There "
 *
 * scala> "Hey " + s + "!"
 * res5: String = Hey  There !
 *
 * scala> "Hey " + s.norm + "!"
 * res6: String = Hey there!
 * 
*/ trait NormMethods { /** * Class containing a norm method that normalizes the given object o of type T * via the implicitly passed Normalization[T]. */ final class Normalizer[T](o: T)(implicit normalization: Normalization[T]) { /** * Normalizes the object o of type T via the implicitly passed Normalization[T] passed * to the constructor of this Normalizer. * * @return a normalized form of o */ def norm: T = normalization.normalized(o) } import scala.language.implicitConversions /** * Implicit conversion that adds a norm method to a value of any type T for which * an implicit Normalization[T] exists. * * @param o the object to convert * @return a Normalizer that enables a norm method to be invoked on the passed object */ implicit def convertToNormalizer[T](o: T)(implicit normalization: Normalization[T]): Normalizer[T] = new Normalizer[T](o) } /** * Companion object for NormMethods enabling its members to be imported as an alternative to mixing them in. */ object NormMethods extends NormMethods




© 2015 - 2024 Weber Informatics LLC | Privacy Policy