Quantcast

2.11.x -> 2.12.x problem

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

2.11.x -> 2.12.x problem

Johan Piculell
Hi.
Bear with me here because I'm no Scala developer, just tried to update from 2.11 to 2.12.x in a project and ran into a problem that I cannot figure out. 
I stripped down my quite complex code issue to this simple example:

class Base {
}

trait Form {
  def component: Base
}

class Hello extends Form {
  val component = new Base() {
    var label = "test"
  }

  def print() {
    println(component.label)
    component.label = "set"
    println(component.label)
  }
}

object Test {
  def main(args: Array[String]) {
    new Hello().print()
  }
}


This compiles fine with 2.11 and all the way up to 2.12.0-M5 but fails on 2.12.0-RC1 and onwards:

test.scala:16: error: value label is not a member of Base
    println(component.label)
                      ^
test.scala:17: error: value label is not a member of Base
    component.label = "set"
              ^
test.scala:18: error: value label is not a member of Base
    println(component.label)

Using -Xsource:2.11 also removes the error. 

Do we have an invalid construct in our code or did something unintentionally break in any of the RC1 changes? To me this is a plausible change for breaking our code: https://github.com/scala/scala/pull/5307
But I'm not skilled enough in Scala to even understand what that change exactly means. 

Regards
/Johan

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 2.11.x -> 2.12.x problem

Jasper-M
Hi Johan,

In Scala 2.12.x inference of overridden fields has changed. The release notes can explain better than me: http://scala-lang.org/news/2.12.0#inferred-types-for-fields

A quick fix for your code would be

val component: Base { var label: String } = new Base() {
  var label = "test"
}

Kind regards,
Jasper

Op woensdag 1 maart 2017 16:14:12 UTC+1 schreef Johan Piculell:
Hi.
Bear with me here because I'm no Scala developer, just tried to update from 2.11 to 2.12.x in a project and ran into a problem that I cannot figure out. 
I stripped down my quite complex code issue to this simple example:

class Base {
}

trait Form {
  def component: Base
}

class Hello extends Form {
  val component = new Base() {
    var label = "test"
  }

  def print() {
    println(component.label)
    component.label = "set"
    println(component.label)
  }
}

object Test {
  def main(args: Array[String]) {
    new Hello().print()
  }
}


This compiles fine with 2.11 and all the way up to 2.12.0-M5 but fails on 2.12.0-RC1 and onwards:

test.scala:16: error: value label is not a member of Base
    println(component.label)
                      ^
test.scala:17: error: value label is not a member of Base
    component.label = "set"
              ^
test.scala:18: error: value label is not a member of Base
    println(component.label)

Using -Xsource:2.11 also removes the error. 

Do we have an invalid construct in our code or did something unintentionally break in any of the RC1 changes? To me this is a plausible change for breaking our code: <a href="https://github.com/scala/scala/pull/5307" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fscala%2Fscala%2Fpull%2F5307\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGnJRus7lCb1qxSn51UblB53cUZCQ&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fscala%2Fscala%2Fpull%2F5307\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGnJRus7lCb1qxSn51UblB53cUZCQ&#39;;return true;">https://github.com/scala/scala/pull/5307
But I'm not skilled enough in Scala to even understand what that change exactly means. 

Regards
/Johan

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 2.11.x -> 2.12.x problem

Johan Piculell
Thanks for the help Jasper! I suspected that change for quite some time but find it hard to translate this to my problem.
You proposed fix works for the example of course, but my real life example is much more complex involving larger java inheritance hierarchies and same simple change does not work here. 

But I assume then that this is a deliberate change in the scala compiler and that we need to rewrite our code in some way then if upgrading to 2.12.x.

Regards
/Johan

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 2.11.x -> 2.12.x problem

Dennis Haupt
so if i understand correcty, before

class A
class B extends A

trait Base {
   val x:A
}

class Sub extends Base {

}

2017-03-02 8:13 GMT+01:00 Johan Piculell <[hidden email]>:
Thanks for the help Jasper! I suspected that change for quite some time but find it hard to translate this to my problem.
You proposed fix works for the example of course, but my real life example is much more complex involving larger java inheritance hierarchies and same simple change does not work here. 

But I assume then that this is a deliberate change in the scala compiler and that we need to rewrite our code in some way then if upgrading to 2.12.x.

Regards
/Johan

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 2.11.x -> 2.12.x problem

Dennis Haupt
accidently sent too soon...

so if i understand correcty, before

class A
class B extends A

trait Base {
   val x:A
}

class Sub extends Base {
   override val x = new B
}

in sub, x will return an A instead of a B in scala 2.12?

2017-03-02 8:58 GMT+01:00 Dennis Haupt <[hidden email]>:
so if i understand correcty, before

class A
class B extends A

trait Base {
   val x:A
}

class Sub extends Base {

}

2017-03-02 8:13 GMT+01:00 Johan Piculell <[hidden email]>:
Thanks for the help Jasper! I suspected that change for quite some time but find it hard to translate this to my problem.
You proposed fix works for the example of course, but my real life example is much more complex involving larger java inheritance hierarchies and same simple change does not work here. 

But I assume then that this is a deliberate change in the scala compiler and that we need to rewrite our code in some way then if upgrading to 2.12.x.

Regards
/Johan

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 2.11.x -> 2.12.x problem

Dennis Haupt
i tried this:

class A
class B extends A

trait Base {
def x:A
}

val sub = new Base {
override val x = new B
def hello = "world"
}

val b:B = sub.x
sub.hello

and it works fine in 2.12
x return a B, not an A, and sub.hello also works. i do not see the difference to the non working case....
can someone explain this?

2017-03-02 8:59 GMT+01:00 Dennis Haupt <[hidden email]>:
accidently sent too soon...

so if i understand correcty, before

class A
class B extends A

trait Base {
   val x:A
}

class Sub extends Base {
   override val x = new B
}

in sub, x will return an A instead of a B in scala 2.12?

2017-03-02 8:58 GMT+01:00 Dennis Haupt <[hidden email]>:
so if i understand correcty, before

class A
class B extends A

trait Base {
   val x:A
}

class Sub extends Base {

}

2017-03-02 8:13 GMT+01:00 Johan Piculell <[hidden email]>:
Thanks for the help Jasper! I suspected that change for quite some time but find it hard to translate this to my problem.
You proposed fix works for the example of course, but my real life example is much more complex involving larger java inheritance hierarchies and same simple change does not work here. 

But I assume then that this is a deliberate change in the scala compiler and that we need to rewrite our code in some way then if upgrading to 2.12.x.

Regards
/Johan

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.



--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 2.11.x -> 2.12.x problem

Jasper-M
In reply to this post by Johan Piculell
Compiling with -Xsource:2.11 might be the best temporary solution then.

Dennis's example has confused me a bit as to how inference actually works in 2.12 for overriding fields.
But what I take from it is that your easiest permanent solution is probably refactoring code like this

val component = new Base() {
  var label = "test"
}

to this:

class BaseWithLabel extends Base {
  var label = "test"
}
val component = new BaseWithLabel

Because apparently inference didn't change when using named subclasses.


Kind regards,
Jasper

Op donderdag 2 maart 2017 08:13:24 UTC+1 schreef Johan Piculell:
Thanks for the help Jasper! I suspected that change for quite some time but find it hard to translate this to my problem.
You proposed fix works for the example of course, but my real life example is much more complex involving larger java inheritance hierarchies and same simple change does not work here. 

But I assume then that this is a deliberate change in the scala compiler and that we need to rewrite our code in some way then if upgrading to 2.12.x.

Regards
/Johan

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 2.11.x -> 2.12.x problem

oss.mlists
"Because apparently inference didn't change when using named subclasses."

Is it intended or bug?

Petr

On Thursday, March 2, 2017 at 11:24:55 AM UTC+1, Jasper-M wrote:
Compiling with -Xsource:2.11 might be the best temporary solution then.

Dennis's example has confused me a bit as to how inference actually works in 2.12 for overriding fields.
But what I take from it is that your easiest permanent solution is probably refactoring code like this

val component = new Base() {
  var label = "test"
}

to this:

class BaseWithLabel extends Base {
  var label = "test"
}
val component = new BaseWithLabel

Because apparently inference didn't change when using named subclasses.


Kind regards,
Jasper

Op donderdag 2 maart 2017 08:13:24 UTC+1 schreef Johan Piculell:
Thanks for the help Jasper! I suspected that change for quite some time but find it hard to translate this to my problem.
You proposed fix works for the example of course, but my real life example is much more complex involving larger java inheritance hierarchies and same simple change does not work here. 

But I assume then that this is a deliberate change in the scala compiler and that we need to rewrite our code in some way then if upgrading to 2.12.x.

Regards
/Johan

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 2.11.x -> 2.12.x problem

oss.mlists
In reply to this post by Jasper-M
"Because apparently inference didn't change when using named subclasses."

Is it intended or bug?

Petr

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 2.11.x -> 2.12.x problem

Jasper-M
I think it's intended, because I see the same behaviour for defs, and the release notes say "Type inference for val, and lazy val has been aligned with def".
"the type of the overridden field is used as the expected type" probably does not mean that the inferred type needs to be the same as the type of the overridden field.

Op donderdag 2 maart 2017 11:30:30 UTC+1 schreef [hidden email]:
"Because apparently inference didn't change when using named subclasses."

Is it intended or bug?

Petr

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 2.11.x -> 2.12.x problem

Johan Piculell
In reply to this post by Jasper-M
Thanks Jasper. 
I followed your subclass example and updated all our code and at least it compiles now, so lets see how regression tests looks tomorrow.

Regards
/Johan

On Thursday, March 2, 2017 at 11:24:55 AM UTC+1, Jasper-M wrote:
Compiling with -Xsource:2.11 might be the best temporary solution then.

Dennis's example has confused me a bit as to how inference actually works in 2.12 for overriding fields.
But what I take from it is that your easiest permanent solution is probably refactoring code like this

val component = new Base() {
  var label = "test"
}

to this:

class BaseWithLabel extends Base {
  var label = "test"
}
val component = new BaseWithLabel

Because apparently inference didn't change when using named subclasses.


Kind regards,
Jasper

Op donderdag 2 maart 2017 08:13:24 UTC+1 schreef Johan Piculell:
Thanks for the help Jasper! I suspected that change for quite some time but find it hard to translate this to my problem.
You proposed fix works for the example of course, but my real life example is much more complex involving larger java inheritance hierarchies and same simple change does not work here. 

But I assume then that this is a deliberate change in the scala compiler and that we need to rewrite our code in some way then if upgrading to 2.12.x.

Regards
/Johan

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 2.11.x -> 2.12.x problem

Stephen Compall-3
In reply to this post by Jasper-M

On 3/2/17 5:24 AM, Jasper-M wrote:

class BaseWithLabel extends Base {
  var label = "test"
}
val component = new BaseWithLabel

In the given test, this can be shortened to

  object component extends Base {
    var label = "test"
  }
As with the class solution, and unlike the original code, it will not yield reflective references to label.

--
Stephen Compall

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...