The website uses cookies to optimize your user experience. Using this website grants us the permission to collect certain information essential to the provision of our services to you, but you may change the cookie settings within your browser any time you wish. Learn more
I agree
Summary | 30 Annotations
abstract modifier in front of the class
2018/10/03 11:44
Note that the contents method in class Element does not carry an abstract modifier. A method is abstract if it does not have an implementation
2018/10/03 11:45
Such parameterless methods are quite common in Scala. By contrast, methods defined with empty parentheses, such as def height(): Int, are called empty-paren methods. The recommended convention is to use a parameterless
2018/10/03 13:34
method whenever there are no parameters and the method accesses mutable state only by reading fields of the containing object (in particular, it does not change mutable state).
2018/10/03 13:34
uniform access principle
2018/10/03 13:34
which says that client code should not be affected by a decision to implement an attribute as a field or method.
2018/10/03 13:35
In particular, you can override a parameterless method with an empty-paren method, and vice versa. You can also leave off the empty parentheses on an invocation of any function that takes no arguments.
2018/10/03 13:50
it is recommended to still write the empty parentheses when the invoked method represents more than a property of its receiver object. For instance, empty parentheses are appropriate if the method performs I/O, or writes reassignable variables (vars), or reads vars other than the receiver's fields, either directly or indirectly by using mutable objects.
2018/10/03 13:51
To summarize, it is encouraged style in Scala to define methods that take no parameters and have no side effects as parameterless methods, i.e., leaving off the empty parentheses. On the other hand, you should never define a method that has side-effects without parentheses, because then invocations of that method would look like a field selection.
2018/10/03 13:52
Another way to think about this is if the function you're calling performs an operation, use the parentheses, but if it merely provides access to a property, leave the parentheses off.
2018/10/03 13:52
... extends Element ...
2018/10/03 13:53
If you leave out an extends clause, the Scala compiler implicitly assumes your class extends from scala.AnyRef, which on the Java platform is the same as class java.lang.Object.
2018/10/03 13:54
Another difference is that in Scala, fields and methods belong to the same namespace. This makes it possible for a field to override a parameterless method.
2018/10/03 13:57
in Scala it is forbidden to define a field and method with the same name in the same class,
2018/10/03 13:58
Scala's two namespaces are: values (fields, methods, packages, and singleton objects) types (class and trait names)
2018/10/03 13:58
You can avoid the code smell by combining the parameter and the field in a single parametric field definition, as shown in Listing 10.5:
2018/10/03 14:04
Note that now the contents parameter is prefixed by val.
2018/10/03 14:05
2018/10/03 14:05
You can also prefix a class parameter with var,
2018/10/03 14:05
Finally, it is possible to add modifiers such as private, protected,[5] or override to these parametric fields, just as you can do for any other class member. Consider, for instance, the following class definitions:
2018/10/03 14:05
class LineElement(s: String) extends ArrayElement(Array(s))
2018/10/03 14:07
The modifier is optional if a member implements an abstract member with the same name.
2018/10/03 14:10
In Scala, as in Java, you do this by adding a final modifier to the member
2018/10/03 14:15
To do this you simply declare the entire class final by adding a final modifier to the class declaration.
2018/10/03 14:16
2018/10/03 14:18
The ++ operation concatenates two arrays
2018/10/03 14:18
Specifically, arrays in Scala inherit from a class scala.Seq,
2018/10/03 14:18
The zip method picks corresponding elements in its two arguments and forms an array of pairs. For instance, this expression:
2018/10/03 14:21
Here, the syntax "for ((line1, line2) <- ...)" allows you to name both elements of a pair in one pattern, i.e., line1 stands now for the first element of the pair, and line2 stands for the second.
2018/10/03 14:22
A straightforward solution is to create a companion object of class Element and make this be the factory object for layout elements. That way, you need to expose only the class/object combo of Element to your clients, and you can hide the three implementation classes ArrayElement, LineElement, and UniformElement.
2018/10/03 14:27