Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not set the value directly

I'm reading the source of a project, and found such code there:

private var _responded: Boolean = _
{
    _responded = false
}

I don't understand why he wrote it like this, isn't it the same as:

private var _responded = false

What's the difference between them?

like image 799
Freewind Avatar asked Aug 17 '11 03:08

Freewind


2 Answers

I'm the author of that code.

Writing like this:

private var _responded = false

causes this warning on compilation (with older versions of Scala, seems no problem with Scala 2.9):

the initialization is no longer be executed before the superclass is called

You can google about that warning to find more information.

like image 80
Ngoc Dao Avatar answered Sep 23 '22 09:09

Ngoc Dao


I'm going to hazard a guess here, but that looks a lot like the code produced by intellij's automatic Java to Scala conversion.

This converter tries to maintain the semantics of the original Java as closely as possible, and so tends to produce very non-idiomatic code, as well as lots of nested scopes and mutable variables.

like image 40
Kevin Wright Avatar answered Sep 23 '22 09:09

Kevin Wright