7

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?

4
  • 2
    That's puzzling. I'm wondering if it has something to do with the linearization of multiple inherited traits. Commented Aug 17, 2011 at 4:52
  • 1
    No difference that I know of. Perhaps it would be a good idea to ask the author? Commented Aug 17, 2011 at 5:03
  • Re: multiple inheritence -- scratch that, vars can't be overridden. Commented Aug 17, 2011 at 5:08
  • @Alexey, OK, I will ask the author for this Commented Aug 17, 2011 at 8:12

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

6

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.

2 Comments

Do you know how the semantics differ between the two (var initialized inline, or in a separate block)?
For most intents and purposes they're the same. In come scenarios though, there's an (admittedly small) risk of another thread reading the uninitialised value when the declaration and assignment are discrete statements.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.