Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* SCALA */
- object SampleImplicits {
- // mention object to be extended only once
- implicit def stringWrapper(s: String) = new {
- def sort() = {
- val arr = s.toCharArray()
- java.util.Arrays.sort(arr)
- String.valueOf(arr)
- }
- def checksum() = s.map(_.toInt).fold(0)(_ + _)
- def toUpper() = s.toUpperCase()
- }
- }
- object Sample {
- def main(args: Array[String]) = {
- // use extensions where they are needed
- import SampleImplicits._
- val sorted = "Sort me!".sort()
- println(sorted)
- val summed = "Sum me!".checksum()
- println(summed)
- val string = "Upper me!".toUpper()
- println(string)
- }
- }
Add Comment
Please, Sign In to add comment