Hello!
I’m looking for feedback for a page I’m working on to help people read Scala or Java if they know the other one. Things I’d like help with include errors and omissions.
https://www.jeffshaw.me/cheat/
Thanks
Hello!
I’m looking for feedback for a page I’m working on to help people read Scala or Java if they know the other one. Things I’d like help with include errors and omissions.
https://www.jeffshaw.me/cheat/
Thanks
I’ve purposefully left out most topics involving collections, since it’s too big a topic for a beginner’s reference. It could be another page.
I sent you a pull request with some additions and a fix.
Overall, I think your cheat sheet is good for looking up things if you have used both languages at least some time, but if a Java developer used this cheat sheet while learning Scala, it may cause him to write code that isn’t idiomatic Scala, especially because of the various examples using var. But that problem may be inherent to the cheat sheet format, because the idiomatic solution may not have a direct java equivalent without also changing several other parts of the program. I don’t know if this could be improved.
class C(var i: Int)
isn’t shorthand for
class C {
var i: Int = _
def C(i: Int) {
this.i = i
}
}
The second should be written
class C() {
var i: Int = _
def this(i: Int) = {
this()
this.i = i
}
}
but note that this has two constructors, a no-params one, and a one param one. It’s not syntactic sugar for the former.
I would suggest removing the second entirely, and just list class C(var i: Int)
Then, shouldn’t it be:
class C private () {
var i: Int = _
def this(i: Int) = {
this()
this.i = i
}
}
?
Then it still has two constructors, but one is private. A better alternative would be
class C(i_: Int) {
var i = i_
}
but I don’t see why listing syntactic sugar that’s just a normal constructor would be a good idea.
This is a subtle point. Thanks @martijnhoekstra and @charpov for pointing this out. I’ll just remove the expansion.
It’s sufficiently subtle that I had to consult the spec to be entirely sure, and come to the conclusion that the code example on the spec didn’t compile, so don’t worry about that
A few remarks after a quick look at the sheet:
Interfaces:
interface I {
void method(int i) {}
}
should probably be:
interface I {
void method(int i);
}
Wildcards:
Scala’s [A <: T1 with T2]
would be <A extends T1 & T2>
in Java (using &
, not a comma).
The section on statics is not clear and probably cannot be without getting into companion objects. You need an example of a Java class with bot static and non-static fields/methods.
You could mention the private[this]
of Scala, which has no Java equivalent.
I don’t understand you scoping discussion of switch
. If really there is a single scope in Java (which I doubt), shouldn’t the second int i
declaration be rejected?
Why not use the standard CompletableFuture
in Java?
CompletableFuture<Integer> add(CompletableFuture<Integer> f1, CompletableFuture<Integer> f2) {
return f1.thenCompose(i1 ->
f2.thenApply(i2 ->
i1 + i2
)
);
}
(The async/non-async distinction is more complicated; Java offers more control than Scala there.)
The lambda section reminded me of why I prefer to program in Scala (and that’s without even mentioning that Java’s lambdas cannot throw checked exceptions…)
You might want to check the document for compliance with the Web Content Accessibility Guidelines (WCAG). For example, run it through WebAIM’s WAVE tool http://wave.webaim.org/
As a person who teaches in a US university and produces course materials, I have recently become sensitized to the need to produce materials that are accessible to individuals who use screen readers (e.g for persons with visual impairments) and other assistive technologies.
default
keyword.i
in each one. I think the difference with Scala is that Scala lets you shadow a local variable in an inner scope, which Java doesn’t:// possible in Scala but not in Java
val i = 0;
{
val i = "foo"
}
This makes it impossible in Java to introduce in a switch
case a variable name that is already in the scope of the switch
statement.Are you suggesting someone might use my page for teaching? That’d be very cool.
switch
example there.I will likely put a reference link to it in my course materials for the Scala-based course on functional programming I am teaching beginning in a couple days. … But in any event, it is a good practice to make any document or Web page reasonably accessible to individuals who need to use assistive technologies to consume or interact with the content.