Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


Share this Page URL
Help

Chapter 6: Functional Objects > 6.14 Conclusion - Pg. 113

S ECTION 6.13 · A word of caution find out more on implicit conversions, including ways to bring them into scope where they are needed, in Chapter 21. 6.13 A word of caution As this chapter has demonstrated, creating methods with operator names and defining implicit conversions can help you design libraries for which client code is concise and easy to understand. Scala gives you a great deal of power to design such easy-to-use libraries, but please bear in mind that with power comes responsibility. If used unartfully, both operator methods and implicit conversions can give rise to client code that is hard to read and understand. Because im- plicit conversions are applied implicitly by the compiler, not explicitly writ- ten down in the source code, it can be non-obvious to client programmers what implicit conversions are being applied. And although operator meth- ods will usually make client code more concise, they will only make it more readable to the extent client programmers will be able to recognize and re- member the meaning of each operator. The goal you should keep in mind as you design libraries is not merely enabling concise client code, but readable, understandable client code. Con- ciseness will often be a big part of that readability, but you can take concise- ness too far. By designing libraries that enable tastefully concise and at the same time understandable client code, you can help those client program- mers work productively. 6.14 Conclusion In this chapter, you saw more aspects of classes in Scala. You saw how to add parameters to a class, define several constructors, define operators as methods, and customize classes so that they are natural to use. Maybe most importantly, you saw that defining and using immutable objects is a quite natural way to code in Scala. Although the final version of Rational shown in this chapter fulfills the requirements set forth at the beginning of the chapter, it could still be im- proved. We will in fact return to this example later in the book. For example, in Chapter 30, you'll learn how to override equals and hashcode to allow Rational s to behave better when compared with == or placed into hash ta- 113