Skip to content

Instantly share code, notes, and snippets.

@p3t0r
Created January 29, 2009 19:09
Show Gist options
  • Save p3t0r/54679 to your computer and use it in GitHub Desktop.
Save p3t0r/54679 to your computer and use it in GitHub Desktop.
overloading a unary operator in scala
class MyString(string:String){
var value = string
def unary_- = new MyString(new StringBuilder(value).reverse.toString)
override def toString = value
}
val myString = new MyString("abcd")
println(myString) // prints "abcd"
println(-myString) // prints "dcba"
@mariohuizar
Copy link

mariohuizar commented Apr 16, 2018

class MyString(theString: String) {
  def unary_! = theString.reverse
}

val test = new MyString("abcd")
println(test)  //prints "abcd"
println(!test)  //prints "dcba"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment