Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • 15:27
    smarter closed #12416
  • 15:27
    smarter commented #12416
  • 15:24
    chrislailee opened #12416
  • 07:24
    dwijnand edited #9672
  • 03:01
    jxnu-liguobin commented #2059
  • 02:54
    jxnu-liguobin commented #2059
  • 02:50
    som-snytt edited #9672
  • 02:48
    som-snytt converted_to_draft #9672
  • 02:42
    som-snytt edited #9672
  • 02:28
    Raysphere24 commented #2059
  • 02:11
    scala-jenkins milestoned #9672
  • 02:11
    som-snytt opened #9672
  • 01:34
    SethTisue commented #2059
  • 01:34
    SethTisue commented #2059
  • 01:10
    SethTisue commented #9488
  • 01:07
    SethTisue commented #771
  • 01:06
    som-snytt commented #771
  • 01:00
    retronym commented #771
  • 00:54
    SethTisue edited #771
  • 00:54
    SethTisue edited #771
Michał Pałka
@prolativ
@your-psychiatrist:ellipsen.net metals' gitter channel would be the best place to ask but I believe you need to place your script with other sources inside an sbt/mil/... project so that your IDE knows e.g. which version of scala to use to try to compile your code
DavidGoodenough
@DavidGoodenough
In a Scala-3 macro, if I have a type variable t (obtained from a (using t:Type[T]) argument) how would build an Expr which is an invocation of T's constructor or an apply method on its companion object? I think I can see how to do it by building the Expr manually, but how do I do it using quotes and splices?
Nik
@your-psychiatrist:ellipsen.net
[m]
@prolativ ah yes thank you, will hop over
Luis Miguel Mejía Suárez
@BalmungSan
@your-psychiatrist:ellipsen.net you can set the Scala version of the script with a small comment like // scala 2.13.6
1 reply
This message was deleted
At the beginning of the file.
And remember the files should be..sc rather than . scala
1 reply
Nik
@your-psychiatrist:ellipsen.net
[m]
well so scala 2 works flawlessly anyway, i just have a *.sc file and everything is nicely linted
scala 3 completely breaks but i am talkign to the metals people :>
Luis Miguel Mejía Suárez
@BalmungSan
I am not sure if there is an official release of ammonite for Scala 3 yet.
Nik
@your-psychiatrist:ellipsen.net
[m]
afaik there isn't
ammonite 2.4.0 has "internal" support for scala 3
YulawOne
@YulawOne

Hey everyone, I was playing with scala 3 type level programming and implemented Contains type like this:

type Is[A, B] = A match
    case B => true
    case _ => false

  type Contains[A <: Tuple, B] <: Boolean = Tuple.Filter[A, [X] =>> Is[X, B]] match
    case EmptyTuple => false
    case _ => true

  inline given test[A <: Tuple, B]: Contains[A, B] = null.asInstanceOf[Contains[A, B]]

  def contains[A <: Tuple, B](tuple: A)(using Contains[A, B] =:= true) =
    true

  def main(args: Array[String]): Unit =
    println(contains[(Int, String), String]((1, ""))) // ok
    println(contains[(Int, String), Boolean]((1, ""))) // fail

it works as expected, but I tried to simplify it to:

import scala.compiletime.ops.any._
type Is[A, B] = A == B

  type Contains[A <: Tuple, B] <: Boolean = Tuple.Filter[A, [X] =>> Is[X, B]] match
    case EmptyTuple => false
    case _ => true

  inline given test[A <: Tuple, B]: Contains[A, B] = null.asInstanceOf[Contains[A, B]]

  def contains[A <: Tuple, B](tuple: A)(using Contains[A, B] =:= true) =
    true

  def main(args: Array[String]): Unit =
    println(contains[(Int, String), String]((1, ""))) // fails too while shouldn't
    println(contains[(Int, String), Boolean]((1, ""))) // fail

it started to failing for both cases, so question - is there any difference between

type Is[A, B] = A match
    case B => true
    case _ => false

and

type Is[A, B] = A == B

Or it can be a bug?

Guillaume Martres
@smarter
@frekw ah yeah that definitely looks broken, I think it's worth checking with a nightly if this is fixed (scalaVersion := "3.0.2-RC1-bin-20210617-af3703b-NIGHTLY"), otherwise we'd welcome a bug report
@YulawOne yes, see the documentation of ==, it's only for singleton types: http://dotty.epfl.ch/api/scala/compiletime/ops/any$.html
YulawOne
@YulawOne
Got it, thanks
Guillaume Martres
@smarter
@frekw there's a good chance this is the same issue as lampepfl/dotty#7524
Jim Newton
@jimka2001
can someone help me understand how pattern matching an Int works in Scala. I don't find an unapply method in the Int.scala source code. Is it a special case understood by the compiler?
Guillaume Martres
@smarter
@jimka2001 case x: Int => is a type pattern, case 1 => is a literal pattern, for details see https://www.scala-lang.org/files/archive/spec/2.13/08-pattern-matching.html
(and usages of unapply are extractor patterns)
Jim Newton
@jimka2001
follow up: Is there a way to convert the following function into something more fundamental than pattern matching?
  def intp(a:Any):Boolean = {
    a match {
      case _:Int => true
      case _ => false
    }
  }
Rob Norris
@tpolecat
I think = a.isInstanceOf[Int] would be equivalent here.
Jim Newton
@jimka2001
no, experimentation shows that IS NOT equivalent
Rob Norris
@tpolecat
What's the difference you see?
Jim Newton
@jimka2001
ahhh. maybe I'm confused. I know that the following is not equivlent. classOf[Int].isInstance(1) returns false. which is not what you said @tpolecat
seems like classOf[Int].isInstance(1) and 1.isInstanceOf[Int] are very different questions.
Rob Norris
@tpolecat
Int is not a class type, although Scala blurs this somewhat.
The latter is what you want.
Eric K Richardson
@ekrich
@jd557:matrix.org I have been trying to work on some math stuff for Scala Native. https://github.com/ekrich/sblas
João Costa
@jd557:matrix.org
[m]
yeah, I've seen that in the past, but I can't use scala native here :P
Jim Newton
@jimka2001
blurs? why do the following both evaluate to true? 1.isInstanceOf[Any] and classOf[Any].isInstance(1) ? Is that because of the blur or is there an understandable reason?
Eric K Richardson
@ekrich
Oh ok, maybe eventually Scala Native will be to a place that people are willing to use it - a bit slow but making progress.
Rob Norris
@tpolecat
@jimka2001 the JVM has primitives, and wrapped versions that are reference types (int and Integer in Java). Scala sweeps this distinction under the covers. Sometimes you're operating on a reference type, sometimes you're operating on a primitive.
When you say classOf[Int] you're getting Integer.TYPE which is a synthetic class that represents the primitive int type. isInstance takes Object which is by definition not a primitive, so it always returns false.
This is all JDK stuff, not a Scala thing. If you're messing with classes all of Scala's fancy footwork to hide this distinction is lost.
@ case class Box(value: Any) 
defined class Box

@ Box(1).value.getClass 
res10: Class[T] = class java.lang.Integer

@ 1.getClass 
res11: Class[Int] = int
Rob Norris
@tpolecat
But .isInstanceOf is a Scala method so it can be smart.
@ (1:Any).isInstanceOf[Integer] 
res15: Boolean = true

@ (1:Any).isInstanceOf[Int] 
res16: Boolean = true
@ classOf[Int] == classOf[Integer] 
res17: Boolean = false
Scala works real hard to hide this stuff and it does a pretty amazing job.
Jim Newton
@jimka2001
classOf[java.lang.Integer].isInstance(1) returns true
Rob Norris
@tpolecat
That's right, because Scala boxes the 1 for you.
Jim Newton
@jimka2001
so is x => classOf[java.lang.Integer].isInstance(x) the same as
  def intp(a:Any):Boolean = {
    a match {
      case _:Int => true
      case _ => false
    }
  }
Rob Norris
@tpolecat
In effect it would be, but .isInstanceOf[Int] is probably what you want.
Jim Newton
@jimka2001
No, what my program is doing is taking types, manipulating them and eventually calling the .isInstance method. or some extension thereof such as seq.exists(x => x.isInstance(y))
seq.forall(x => x.isInstance(y)
Rob Norris
@tpolecat
If you're trying to abstract over that by passing a class then you're probably playing with fire.
It sounds like a design problem to me. I know you're very deep into whatever you're doing so classOf[Integer] may get you out of this particular hole, but programming with Any is treacherous.
Jim Newton
@jimka2001
If what i'm trying to do eventually doesn't work, but I understand why it fails, I'll also consider that a success.
Rob Norris
@tpolecat
:-)
Jim Newton
@jimka2001
@tpolecat its bizarre thatclassOf[Integer].isInstance(1)`. I was convinced I had already tried that at it failed.
Many thanks for the help.