Let's say I want to add a function to the String class to 'calibrate' data for comparison, eg remove trailing whitespace/convert to lowercase/remove punctuation
I want to call it like " StrING ".calibrate which would give me "string"
Then from another class
I want to call it like " StrING ".calibrate which would give me "string"
import scala.language.implicitConversions
object StringCalibrationImplicits {
  class CalibratedString(s: String) {
    def calibrate = {
      // also might wanna filter out punctuation marks or whatever      s.trim.toLowerCase
    }
  }
  implicit def calibrate(s: String): CalibratedString = new CalibratedString(s)
  
}
Then from another class
import com.ojha.StringCalibrationImplicits._
val s = "  StrING   "s.calibrate
 
No comments:
Post a Comment