by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 14:30
    bishabosha edited #9549
  • 14:25
    bishabosha synchronize #9549
  • 14:21
    liufengyun commented #9562
  • 14:21
    liufengyun commented #9562
  • 14:15
    smarter labeled #9567
  • 14:14
    smarter labeled #9566
  • 14:11
    djspiewak labeled #9567
  • 14:11
    djspiewak opened #9567
  • 14:09
    djspiewak labeled #9566
  • 14:09
    djspiewak opened #9566
  • 13:45
    nicolasstucki synchronize #9553
  • 13:37
    liufengyun commented #9565
  • 13:35
    smarter commented #9565
  • 13:29
    scalavision commented #9473
  • 13:28
    odersky synchronize #9561
  • 13:04
    nicolasstucki synchronize #8940
  • 13:01
    smarter synchronize #9542
  • 12:54
    bishabosha commented #7227
  • 12:54
    dotty-bot commented #9561
  • 12:51
    liufengyun opened #9565
Nicolas Rinaudo
@nrinaudo
actually, how does it know that the custom unapply will not fail on Some, even if it returns a Some?
Guillaume Martres
@smarter
Because it takes Some as input
this would be less confusing if you were matching on something other than Option :)
Nicolas Rinaudo
@nrinaudo
it's a bit confusing, yes :)
let me try and play with it a bit more
Nicolas Rinaudo
@nrinaudo

If I change my unapply to:

    def unapply[A](o: Option[A]): Some[A] = o match {
      case s: Some[A] => s
      case _ => sys.error("fail")
    }

I don't get a warning. I understand that as far as the types are concerned, this makes sense, but how does the compiler know without special handling of Some?

Guillaume Martres
@smarter
this is too confusing to explain when you're matching on an Option
Nicolas Rinaudo
@nrinaudo
OK. Is it alright to still use an Option or a Some as the return type though?
Guillaume Martres
@smarter
here's a better example:
sealed trait Foo[A]
case class One[A](a: A) extends Foo[A]
case class Two[A](a: A) extends Foo[A]

object ExtractOne {
  def unapply[A](s: One[A]): Some[A] = Some(s.a)
}

def unwrap(fi: Foo[Int]): Int = fi match {
  case ExtractOne(i) => i
}
This will complain about a missing case for Two
Nicolas Rinaudo
@nrinaudo
It does indeed
Guillaume Martres
@smarter
Because the unapply cannot fail, since it returns a Some it will always be used if the input has type One
Nicolas Rinaudo
@nrinaudo
isn't that making assumptions about unapply's body, such as the fact that it doesn't use reflection?
Guillaume Martres
@smarter
I'm not sure what you mean by that
Nicolas Rinaudo
@nrinaudo
probably nothing intelligent. Let's ignore it.
Guillaume Martres
@smarter
The unapply is a method that takes a One and returns a Some
Nicolas Rinaudo
@nrinaudo
but then - is it the Some type, or the shape of the type?
Guillaume Martres
@smarter
we know that an unapply succeeds if it returns a Some
if we know statically that it returns a Some then we know statically that it cannot fail
Nicolas Rinaudo
@nrinaudo

we know that an unapply succeeds if it returns a Some

This is what I'm struggling with. I thought dotty's pattern matching didn't rely on Option

Guillaume Martres
@smarter
Some is special-cased exactly for this reason.
Nicolas Rinaudo
@nrinaudo
OK, that was exactly what I was struggling with
Option-less pattern matching
Nicolas Rinaudo
@nrinaudo
this lead me to believe that pattern matching didn't rely on Option, when it meant it can work without Option
Guillaume Martres
@smarter
yes
Nicolas Rinaudo
@nrinaudo
ok, so that clarifies everything. I was stuck by the fact that it magically worked without special casing Some
thank you for your time and explanations
Guillaume Martres
@smarter
:)
it's true that http://dotty.epfl.ch/docs/reference/changed-features/pattern-matching.html is missing documentation on the concept of an irrefutable apply
Nicolas Rinaudo
@nrinaudo
I would actually really like to know more about that, and how the behaviour changed since Scala 2. It clearly changed, and I'd love to know to what extent
Guillaume Martres
@smarter
Opened an issue: lampepfl/dotty#6490
Nicolas Rinaudo
@nrinaudo
the code you linked clarifies it quite a bit as well, thanks
I have another, entirely unrelated question. Is there a particular reason for enum types not to extend Serializable and, when all cases are products, Product?
Guillaume Martres
@smarter
no particular reason
Nicolas Rinaudo
@nrinaudo
is this something that could be considered?
scala> enum Foo {
     |   case Bar(i: Int)
     |   case Baz(b: Boolean)
     | }
     | 
     | List(new Foo.Bar(1), new Foo.Baz(true))
// defined class Foo
val res1: List[Foo & Product & Serializable] = List(Bar(1), Baz(true))
I was really hoping for the Product & Serializable wart to disappear :/
Guillaume Martres
@smarter
if you don't use new then you get the apply companion that returns Foo so you just have a List[Foo]
Nicolas Rinaudo
@nrinaudo
absolutely. The wart is better hidden, but it's still here
Guillaume Martres
@smarter
but if no one uses new, then is it really worth the effort to special-case Serializable and Product in enums ?
it'd be more interesting to do this for regular sealed traits
but then it still seems a weird special-casing than you cannot opt out of
Nicolas Rinaudo
@nrinaudo
if it's complicated, probably not. But if not, and you already have a special case for case classes, why not special case ADTs?
Guillaume Martres
@smarter
what do you mean by special case for case classes ?
Nicolas Rinaudo
@nrinaudo
case classes are Product with Serializable, right?
Guillaume Martres
@smarter
it's not complicated to implement, it's just irregular
yes
but that's unconditional
Nicolas Rinaudo
@nrinaudo
I'm arguing Serializable should then be unconditional for enums