def something(...) = ... match {
case Something() => ...
case SomethingElse() => ...
case _ => ...
}
def something(...) = // don't be afraid of adding line breaks
... match {
case Something() => ...
case SomethingElse() => ...
case _ => ...
}
... match {
case Something() => ...
case SomethingElse() =>
...
...
}
... match {
case Something() =>
...
case SomethingElse() =>
...
...
}
val lst = ... val elem = ... val init = ... val col = ... val list = ... val element = ... val initialize = ... val column = ...
class Component {
def getName = ...
def setName(...) { ... }
def isValid = ...
def toHTML = ...
}
if (component.isValid) component.toHTML
class Component {
def name = ...
def name_=(...) { ... }
def valid = ...
def html = ... // "to" was obvious
}
if (component.valid) component.html
class Table {
def getRowColor = ...
def getRowFont = ...
def getColumnColor = ...
def getColumnFont = ...
}
table.getRowColor
class Table {
object row {
def color = ...
def font = ...
}
object column {
def color = ...
def font = ...
}
}
table.row.color
abstract class Mapped
class MappedString extends Mapped
new MappedString // looks like Hungarian notation!
abstract class Mapped
object Mapped {
class String extends Mapped // don't be afraid of reusing existing class names
}
new Mapped.String
(P.s. I agree about having 'set' be explicit; it makes things more
readable. Either that, or do some magic with overloading the assignment
operator with some implicit defs)
Ryan
class Foo {
private var something: Int = _
def bar_=(i: Int) { something = i }
}
allows you to write
val f = new Foo
f.bar = 3
It is the official "setter syntax" in Scala.
> Please forgive my ignorance.. But, where does that "_" notation come
> from?
Actually, there is an underscore in the method name only because the
following character, "=", is a special character and that all special
characters have to be preceded by an underscore. As you can see, the "_"
is only required in the method definition.
Cheers,
Julien
But you are right, when these guidelines will be finished, I will
definitely post them on the Scala mailing list.
Cheers,
Julien
Off the top of my head, here's some questions that could be addressed
1. when to use "for" expressions, and when to use map on Iteratable?
2. when to use abstract classes versus traits
3. when to use case vs not-case classes (other than obvious pattern
matching uses)
4. val vs var
5. when is operator overloading appropriate
6. when are implicit defs appropriate?
7. when to use type members versus generic type parameters
8. brevity/functional style an explanation on the benefits, blocks like
this:
def z = {
val x = someValue
val y = x * someOtherValue
y
}
vs
def z = someValue * someOtherValue
9. when in general to use pattern matching vs. method dispatch
10. pattern matching vs. i.e. visitor pattern
11. various other patterns that are meaningful in java world but made
somewhat obsolete by scala language facilities
Ryan
* Package naming conventions different from Java because a single
package can contain multiple classes.. what's the right way? Plural,
singular? (org.foo.Expressions or org.foo.Expression); What should it
refer to?
* What belongs in a package together?
Ryan
These are really good questions, thank you very much. I will definitely
use them as a starting point for the second part of my guidelines.
Cheers,
Julien