timowest / scalagen

Java to Scala transformation
Apache License 2.0
217 stars 32 forks source link

bad _ value in case of assignment by an if block #42

Closed jmvanel closed 11 years ago

jmvanel commented 11 years ago

The Java source (line 439 ) :

    boolean success;
    ArrayList<Term> list = t1.cloneComponents();
    if (t1.getClass() == t2.getClass()) {
        success = list.addAll(((CompoundTerm) t2).getComponents());
    } else {
        success = list.add(t2);
    }

https://open-nars.googlecode.com/svn/trunk/nars/language/CompoundTerm.java

The generated Scala :

    var success: Boolean = _
    val list = t1.cloneComponents()
    success = if (t1.getClass == t2.getClass) list.addAll(t2.asInstanceOf[CompoundTerm].getComponents) else list.add(t2)

COMMENT: moving up the val list declaration would be ideal solution, but maybe hard to implement ; maybe instead initialization to a neutral value like false for a Boolean and "" for a String will do . But NOTE : maybe this is intentional, because fixing like this would not be safe .

generated Scala, hand edited : https://open-nars.googlecode.com/svn/trunk/nars_core/src/main/scala/nars/language/CompoundTerm.scala

timowest commented 11 years ago

For that the pattern matching would need to be quite invasive. We would need to ensure that success is not assigned anything after the if/else block.

jmvanel commented 11 years ago

It's not clear whether you write about the first , or the second fix I suggested. Anyway I don't see problem with assignments further down.

The fix I applied manually is :

val list = t1.cloneComponents()
var success =
    if (t1.getClass == t2.getClass) list.addAll(t2.asInstanceOf[CompoundTerm].getComponents)
        else list.add(t2)
(if (success) make(t1, list, memory) else null)

The corresponding Java is line 439 : https://open-nars.googlecode.com/svn/trunk/nars/language/CompoundTerm.java

timowest commented 11 years ago

Sorry, I was unclear. The default values for primitives have been fixed now. I might give the if/else merging a try later, but you can also try, if you are comfortable with that.

timowest commented 11 years ago

Released in 0.2.2