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
econoraptor
@econoraptor

ok, so what about this?

type A <: Int
object A {
  def apply(i: Int): A = i.asInstanceOf[A]
}

val isIntA = 3.isInstanceOf[A]

First, the compiler warns me that the type test for A cannot be checked at runtime so I'm curious about that. Second, it tells me that 3 is an A, which surprised me.

Rob Norris
@tpolecat
A is not a class type.
That should be an error. It will probably tell you “foo” is an A.
Don’t use asInstanceOf, don’t use type patterns.
They are unsound if you ignore the warnings.
And they have deeper problems.
econoraptor
@econoraptor
"foo" is not an A, apparently. But point taken about ignoring warnings. Partly I'm just trying to better understand what's going on internally here since I've been running into enough surprising things that there's clearly some things I don't understand
what's the significance about A not being a class type?
Rob Norris
@tpolecat
It’s an abstract type that is completely erased at runtime. There is no associated class or any other mechanism that would let you tell whether an arbitrary value is an A or not.
The best way to think about this in my opinion is to assume that all types are erased and there is no way to tell what anything is at runtime. This is in fact true in general, but for things with primitive or non-parameterized class types you can look at runtime. But the set of things you can check at runtime is much smaller than the universe of types the compiler understands.
econoraptor
@econoraptor
Ah, I didn't realize that. That definitely helps explain some of the other behavior I was confused by
This is a good article but a bit old. Replace “scalaz” with “cats”.
econoraptor
@econoraptor
This looks good. I'll read it and then probably have a couple more questions that I think might be a bit orthogonal. I've watched a couple of your talks and read some of your posts as I've been getting into scala and they've been super helpful, so thx for that as well.
Rob Norris
@tpolecat
Oh cool. Well this is a good place for questions. We’re moving to Discord so you might try that out as well. https://sca.la/discord iirc
Ya.
And https://sca.la/typeleveldiscord for more FP-focused stuff.
econoraptor
@econoraptor
i was aware of the scala discord, but i'll check out the typelevel one too
Luis Miguel Mejía Suárez
@BalmungSan
I guess the time to revive my old ("gamer") discord profile has come.
econoraptor
@econoraptor
ok, this article has convinced me that reasoning about runtime types is difficult
Rob Norris
@tpolecat
It’s easy if you pretend it’s 100% impossible!
Matthew Rooney
@javax-swing
@Woody88 the issue you were facing is that for a Monad ap was being defined in terms of fmap and fmap was being defined in terms of ap. by overriding the definition of fmap in your Monad trait to be defined in terms of flatMap it fixes the issue

     def fmap[B](f : A => B) :M[B] = ma.flatMap(a => pure(f(a)))
Justin Reeves
@justinallenreeves

I guess the time to revive my old ("gamer") discord profile has come.

@BalmungSan
If you have gmail or protonmail, you can use a alias when you signup for an account ${user}@gmail.com => ${user}+${alias}@gmail.com
The aliases still get sent to you, but they have alias in the TO field so you can properly filter via domain
Some thing I wish I knew about 20 years ago for setting up spam filters.

Woodson Delhia
@Woody88
@javax-swing Thank you. It works. I just want to have a better understanding so because Monad trait was defining ap using fmap it causes the loop. I guess that is because Monad trait is reference to the fmap within Applicative. However, why does @main def main = println(List(1).fmap(_ * 2)) refer to the Monad version of instead of directly using the Functor implementation?
Luis Miguel Mejía Suárez
@BalmungSan
@justinallenreeves :+1:
@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