Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Jan 02 23:58
    @SethTisue banned @fakubishes:nerdsin.space
  • Dec 15 2021 05:01
    som-snytt commented #12516
  • Dec 15 2021 04:38
    SethTisue edited #1312
  • Dec 15 2021 04:38
    SethTisue opened #2273
  • Dec 15 2021 04:31
    jackkoenig opened #12516
  • Dec 15 2021 04:29
    SethTisue edited #1312
  • Dec 15 2021 04:28
    SethTisue edited #1312
  • Dec 15 2021 04:27
    SethTisue labeled #9831
  • Dec 15 2021 04:27
    scala-jenkins milestoned #9831
  • Dec 15 2021 04:27
    SethTisue labeled #9831
  • Dec 15 2021 04:27
    SethTisue opened #9831
  • Dec 15 2021 03:35
    som-snytt commented #11339
  • Dec 15 2021 03:27
    som-snytt labeled #12494
  • Dec 15 2021 03:07
    SethTisue edited #1312
  • Dec 15 2021 03:07
    SethTisue edited #1312
  • Dec 15 2021 03:05
    SethTisue edited #1312
  • Dec 15 2021 03:05
    SethTisue edited #1312
  • Dec 15 2021 03:05
    SethTisue edited #1312
  • Dec 15 2021 02:58
    SethTisue edited #1312
  • Dec 15 2021 02:58
    SethTisue synchronize #1312
Luis Miguel Mejía Suárez
@BalmungSan
@Woody88 note that you also shouldn't provide the three givens.
You should just provide the instance of the most specific type (in your example Monad)
Woodson Delhia
@Woody88

@BalmungSan Hmm... So, the instance that will always be pass will be the monad instance? What if I have a generalized function that just needs a Functor constraint, how will the compiler find that instance?

For example:

def someGeneralizedFunc [F[_]: Functor](fa: F[String]): F[Int] = fa.fmap(x => 3)

someGeneralizedFunc(List("yo"))  // I don't think this will work with just a Monad instance for List
Luis Miguel Mejía Suárez
@BalmungSan
@Woody88 yes it will find it.
From a theoretical point of view, any type that forms a Monad can form a Functor
Because you can always provide a lawful implementation of map in terms of flatMap and pure
However, in Scala is even simpler than that. Monad[F] extends Functor[F], as such, it is a subtype.
And due Liskov we can always pass a subtype.
Woodson Delhia
@Woody88
@BalmungSan I get that but it's because you Monad directly extends Functor. I have Monad only extends Applicative, and Applicatve extends Functor. I just gave it try and it doesn't work unless I have Monad extends Applicative with Functor.
3 replies
However, isn't it repetitive since Applicative already extends Functor?
Colin Fraizer
@cfraizer
Where can one download the 3.0 API documentation as a tar/zip file?
Swoorup Joshi
@Swoorup
does scala have a dbg! macro as in rust?
heksesang
@heksenlied:matrix.org
[m]
What does this mean when I try to call map on a tuple?
[error]    |                         Found:    Object with PolyFunction {...}
[error]    |                         Required: PolyFunction{apply: [t](x$1: t): Any}
1 reply
heksesang
@heksenlied:matrix.org
[m]
So this kind of error doesn't have a specific meaning? Kinda makes poly functions a pain to use if there is no concise way to interpret compile errors related to them. :S
            summonForT[A].map(
              [r] =>
                (reader: r) =>
                  inline reader match {
                    case reader: ConfigReader[t] =>
                      val key = fieldMapping("field")
                      reader.isInstanceOf[ReadsMissingKeys] match {
                        case true => reader.from(objCur.atKeyOrUndefined(key))
                        case false => objCur.atKey(key).flatMap(reader.from(_))
                      }
                }
            )
where summonForT is an inline method which returns a Tuple.Map[m.MirroredElemTypes, ConfigReader]
heksesang
@heksenlied:matrix.org
[m]
But if there is no good way to interpret the errors, I think it might be better to just not use poly functions, and rather find other ways to achieve my goals.
1 reply
Fabio Epifani
@epifab

I can't figure out why this code wouldn't compile:

case class Wrapper[A <: Singleton](a: A)(using ValueOf[A])

def test[A <: Singleton](blabla: Wrapper[A]): Unit = ()

test(Wrapper("yo"))

and fail with a:

No singleton value available for A
where: A is a type variable with constraint >: ("yo" : String) and <: Singleton

if I extract Wrapper("yo") then it works:
case class Wrapper[A <: Singleton](a: A)(using ValueOf[A])

def test[A <: Singleton](blabla: Wrapper[A]): Unit = ()

val value = Wrapper("yo")
test(value)
is this a bug in Scala 3?
Michał Pałka
@prolativ
@epifab this seems ok. When you call test(value) then the most exact type of value is value.type which is a singleton type but the type of Wrapper("yo") would be Wrapper["yo"] which is not a singleton type by itself
Vojtech Letal
@letalvoj

Hi :wave: I have a question about quotes and splices. In scala2 I was able to create an Expr[CC] for a case class CC(...) which was equivalent to instantiating the class CC.apply(arguments:_*) as

 val symbol = weakTypeOf[CC].typeSymbol
 val constructor = q"reify(${c.mirror.staticModule(symbol.fullName)}).tree"
 q"Apply($constructor, List(..$arguments))"

The thing which I am struggling with is that in scala2 I could interpolate instances of Tree into a quasiquote and reify and use quotes without getting staging level errors.

Please, anyone has a quick hint on how to replicate the three lines of scala2 in scala3? 🙏🏻

Fabio Epifani
@epifab
@prolativ no I don't believe I'm requiring Wrapper to be a singleton, only its type parameter
Michał Pałka
@prolativ
@epifab that's the point so it makes sense that this doesn't compile
Fabio Epifani
@epifab
@prolativ it doesn't make sense to me yet, sorry. why would it fail saying that it can't find a singleton for "yo": String which is, obviously, a singleton?
heksesang
@heksenlied:matrix.org
[m]
test["yo"](Wrapper("yo")) works.
The problem is that the compiler infers the type of "yo" to be String, not "yo".
Fabio Epifani
@epifab
but that's impossible because I'm explicitly declaring a type bound for A <: Singleton
@heksenlied:matrix.org also the error message suggests that the compiler knows that A is indeed a singleton
Michał Pałka
@prolativ

@epifab sorry, I misread

def test[A <: Singleton](blabla: Wrapper[A])

as

def test[A <: Singleton](blabla: A)
Fabio Epifani
@epifab
yes I thought so :)
heksesang
@heksenlied:matrix.org
[m]
@prolativ: Thanks! I will try to see if I do something similar to what you did, if I can extract a more exact error. 🙂
@epifab: Unsure why, but it obviously doesn't interpret the type as "yo" (as manually specifying that type makes it work). But I do mean to recall there has been such a problem mentioned somewhere (some issue on the tracker?) where one had to store things in a val and pass that val to the method for it to type it correctly.
Fabio Epifani
@epifab
@heksenlied:matrix.org yeah, something definitely wrong there. I opened a bug lampepfl/dotty#12583
heksesang
@heksenlied:matrix.org
[m]
The problem that happens is that A in Wrapper is somehow being "altered" by test, which makes summoning ValueOf[A] fails. What seems to also work is moving the using-clause to test, so it obviously is a case of the test-method making the parameter less specific.
Michał Pałka
@prolativ

test(Wrapper("yo")) works if you redefine Wrapper as

case class Wrapper[A <: Singleton](a: A)

so it looks like using ValueOf[A] is the problem here, not Singleton itself

heksesang
@heksenlied:matrix.org
[m]
Yeah, the type of A within test doesn't resolve to "yo", I guess it resolves to String & Singleton instead.
Which means that, yes, it's a singleton, but there is no way to summon a ValueOf for it.
Michał Pałka
@prolativ
This works
case class Wrapper[A <: Singleton](a: A)(using ValueOf[a.type])

def test[A <: Singleton](blabla: Wrapper[A]): Unit = ()

test(Wrapper("yo"))
Fabio Epifani
@epifab
yeah, something is wrong with the type inference. I also just tested it in Scala 2, and this is a regression
it's interesting that this last example works. I think it defeats my understanding of Singleton
Vojtech Letal
@letalvoj

Please, anyone has a quick hint on how to replicate the three lines of scala2 in scala3? 🙏🏻

I will try to give a more specific example. I would like to have an Expr[CC] which is equivalent to instantiating a new instance of CC, yet generic.

    case class CC(i:Int)
    val expected = '{ new CC(1) }

Attempts to generalize that:

    val ts = TypeRepr.of[CC] // or Type[CC] ...
    val attempt1 = '{new ts(1)} // something as simple as this fails
    val attempt2 = Apply(
      Select.unique(New(Ident(TermRef(ts,"???"))),"<init>"),
      List(Literal(IntConstant(1)))
    ).asExprOf[CC] // I have no idea how to instantiate this
Pls, any chance I can create a quote equivalent to instantiating a new class, given I have
  • the necessary constructor parameters (List[Expr[_]])
  • type of the class (Type[CC])
    ? 🙏🏻
Swoorup Joshi
@Swoorup
is there a way to make this tail recursive?
def findGapsForSorted(x: List[Int]): List[Tuple2[Int, Int]] = 
  def solveFor(xs: List[Int]): List[Tuple2[Int, Int]] =
    val midpoint = xs.length/2
    if xs.length > 2 then 
      val first = xs.head
      val last = xs.last
      if (first + xs.length - 1 >= last)
        then List((first, last))
      else 
        solveFor(xs.slice(0, midpoint)) ++ 
        solveFor(xs.slice(midpoint, xs.length))
    else
      xs match 
        case Nil => List.empty
        case List(one) => List((one, one))
        case start :: end :: Nil => 
          if (start + 1) >= end 
          then List((start, end))
          else List((start, start), (end, end))
        case _ => ???

  solveFor(x)
    .foldLeft(List.empty) { (state, el) => 
      state match 
        case h :+ (start, end) if end + 1 == el._1 => h :+ (start, el._2)
        case xs => xs :+ el
    }
Rob Norris
@tpolecat
Yes, but it's probably not worth it. Stack depth is on the order of log2(xs.length) which can never be larger than 32.
However you're doing a lot of O(N) operations here (.length, ++, :+) that might make this routine slow for large inputs.
Graham Brown
@geronimus

Hello there!

Is there some way to get at an object's runtime TypeTag in Scala 3?

(I'm not using macros. I just want to output it, in order to provide a helpful message in some unit tests.)

Is it safe to add the 2.13 version of the scala-reflect API to my libraryDependencies with cross-compilation?

Or is there a better way of doing this in Scala 3?

Rob Norris
@tpolecat
You just want the name of a type for diagnostic purposes?
Graham Brown
@geronimus
Yes, that's it!
Rob Norris
@tpolecat
This may do what you want https://github.com/tpolecat/typename
It works in Scala 2 and3