Open He-Pin opened 1 year ago
//> using dep io.netty:netty-all:4.1.94.Final
import io.netty.bootstrap._
import io.netty.channel._
import scala.util.Properties.isWin
class C {
val bootstrap = new ServerBootstrap()
val sockaddr = 42
def f(): Unit =
bootstrap
.option(ChannelOption.SO_REUSEADDR, !isWin)
.option(ChannelOption.TCP_NODELAY, true)
.bind(sockaddr)
.channel()
}
then
➜ t12818 scala-cli compile t12818.scala
➜ t12818 scala-cli compile --scala 2 t12818.scala
Compiling project (Scala 2.13.10, JVM)
[error] ./t12818.scala:14:13
[error] type mismatch;
[error] found : io.netty.channel.ChannelOption[Boolean]
[error] required: io.netty.channel.ChannelOption[Any]
[error] Note: Boolean <: Any, but Java-defined class ChannelOption is invariant in type T.
[error] You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)
[error] .option(ChannelOption.SO_REUSEADDR, !isWin)
[error] ^^^^^^^^^^^^^^^^^^^^^^^^^^
Error compiling project (Scala 2.13.10, JVM)
Compilation failed
or
➜ t12818 scala-cli compile t12818.scala -O -Vprint:typer
Compiling project (Scala 3.2.2, JVM)
[[syntax trees at end of typer]] // .../t12818/t12818.scala
package <empty> {
import io.netty.bootstrap.*
import io.netty.channel.*
import scala.util.Properties.isWin
class C() extends Object() {
val bootstrap: io.netty.bootstrap.ServerBootstrap =
new io.netty.bootstrap.ServerBootstrap()
val sockaddr: Int = 42
def f(): Unit =
{
this.bootstrap.option[Boolean](
io.netty.channel.ChannelOption.SO_REUSEADDR
, boolean2Boolean(util.Properties.isWin.unary_!)).option[Boolean](
io.netty.channel.ChannelOption.TCP_NODELAY
, boolean2Boolean(true)).bind(this.sockaddr).channel()
()
}
}
}
Compiled project (Scala 3.2.2, JVM)
Workaround for Scala 2 is explicit .option[java.lang.Boolean](...)
.
Thanks @som-snytt
@som-snytt do you see any possibility of this progressing in Scala 2? wondering if we should leave the ticket open.
@SethTisue I think we should leave it open, Kotlin will not require a <java.lang.Boolean>
at least, it would be great getting some usability improvements back-ported.
And maybe it can labeled with java-interop
too?
@He-Pin If you're interested in seeing it progress, the best starting point would be to have a standalone reproduction, without any external dependencies or unnecessary code. That would best help us assess whether improvement here is possible.
This issue was why I asked why scala-cli doesn't support -Vprint
. I asked on discord, so of course I didn't get an answer. I thought I'd lodged a ticket, but now I remember I thought, What would Seth say to do? Obviously, ask in a forum first. More the fool, I.
➜ t12818 cat s.scala J.java
import java.util.Optional
class C {
val j = new J
val x = Optional.of(java.lang.Boolean.TRUE)
def f = j.f(x, true)
}
import java.util.Optional;
public class J {
public <T> int f(Optional<T> x, T t) {
return 42;
}
}
There are a couple of type inference tickets I was going to look into, related to applications.
I realize that the Lightbend team may not wish to get out the can opener given the unknown number of worms.
One option for ticket management is to mark won't fix because out of scope. That is, have an out-of-scope label so people can search the tickets. Better yet, milestone it for the mythical 2.14.
@SethTisue https://github.com/He-Pin/scala2booleanBug I have updated one
Where Kotlin can:
With Code when using netty 4:
which gives:
Which compiles if:
or
reproducer: https://github.com/He-Pin/scala2booleanBug master branch