-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
4.1.4.3 Shaping Up 2 (Da Streets) exercise's solution gives wrong result #79
Comments
I just came across this as well and was wondering if anyone knows why using |
Thanks for this issue, all. We'll work out a way to include it in the book text eventually, I promise! I'm not 100% sure on all of the details of why this particular case fails, but it'll definitely be due to Scala's initialisation order. Here's what I do know. When an object is created, the bodies of the classes and traits in its hierarchy are executed in order from supertype to subtype. This includes any statements in the body of the class and the RHS of any The classic bug you see is when the RHS of a class Foo {
val a = b
val b = "Bar"
} In this example, the RHS of I tend to prefer not to define abstract trait Foo {
def bar: String
}
// You can override a def with a val:
class Baz extends Foo {
val bar = "Qux"
}
// Alternative to the above. A case class field is effectively a val:
case class Baz(bar: String) extends Foo In general, the pattern you should use for abstract data types is:
I hope this helps! Cheers, |
I just want to give feedback regarding the "4.1.4.3 Shaping Up 2 (Da Streets)" exercise.
The answer to that exercise gives the wrong result.
I found out that the square's perimeter and area are always returning 0.0
to fix it I changed width and height into def as below.
The text was updated successfully, but these errors were encountered: