Method can take data type as parameter

I am in love and this time in love with Scala ,what a great language .Very elegantly designed  and written i thought i knew everything about method overriding when i came across this loving ,fantastic and a  great feature of Scala that allows one to pass the data type as parameter

Function syntax

def <function-name>[type-name](<parameter-name>:<type-name>) : <type-name> ..


object ParametrizeFuncType {
def main(args:Array[String]){
/**
* prints true and takes String
*/
println(identityfunc[String]("amit":String).isInstanceOf[String])
/**
* prints true and takes int
*/
println(identityfunc[Int](1:Int).isInstanceOf[Int])
/*
* wow! classic way for method overriding made data type as parameter
*/
}
def identityfunc[T](a:T)=a
}

you just need not to write multiple methods taking different data types .Just write once and run it with different types and you too will fall in Love 🙂

1 thought on “Method can take data type as parameter

Leave a Reply

Your email address will not be published. Required fields are marked *