In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "A brief introduction to Scala implicit conversion". In daily operation, I believe many people have doubts about the brief introduction of Scala implicit conversion. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "brief introduction to Scala implicit conversion". Next, please follow the editor to study!
In fact, the core of the implicit conversion of Scala is to define the implicit conversion function, namely implicitconversion function. The defined implicit conversion function will be automatically used by Scala as long as it is introduced into the program written. According to the signature of the implicit conversion function, Scala automatically passes it into the implicit conversion function when it uses the object defined by the parameter type received by the implicit conversion function, converts it to another type of object and returns it. This is "implicit conversion".
It is generally recommended that the name of the implicit conversion function be in the form of "one2one".
The only syntax difference between an implicit conversion function and a normal function is that it starts with implicit, and it's best to define the function return type.
Implicit def double2Int (d:Double): Int= {d.toInt} val v:Int = 3.0println (v)
# # pay attention to details
The function name of the implicit conversion function can be arbitrary, and the implicit conversion has nothing to do with the function name, but only with the function signature (function parameter type and return value type).
There can be multiple implicit functions (that is, a list of implicit functions), but you need to ensure that in the current environment, only one implicit function can be recognized.
Class developer {def select (): Unit = {println ("select data")}} class admin {def delete (): Unit = {println ("delete data")}} object TransformObjectApp {def main (args: Array [String]): Unit = {implicit def deleteData (dev: developer): admin = {new admin} val d = new developer d.select () d.delete ()}
# # implicit value
Implicit values, also known as implicit variables, mark a parameter variable as implicit, so the compiler will search for implicit values in scope as default parameters when the method omits the implicit parameter.
At this point, the study of "A brief introduction to Scala implicit conversion" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.