This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Failing to match on singleton type

3 replies
Chris Marshall
Joined: 2009-06-17,
User offline. Last seen 44 weeks 3 days ago.
This is using 2.9.1 - I can't find any matching issues
scala> object Wufflesdefined module Wuffles
scala> (Some(Wuffles): Option[Wuffles.type]) match { case Some(Wuffles) => println("Woof"); case _ => println("Meow") }<console>:10: error: pattern type is incompatible with expected type; found   : object Wuffles required: Wuffles.type              (Some(Wuffles): Option[Wuffles.type]) match { case Some(Wuffles) => println("Woof"); case _ => println("Meow") }                                                                      ^

The complaint is seems to be that Wuffles is not of Wuffles.type
scala> Wufflesres10: Wuffles.type = Wuffles$@11dc088

OK. Is this a new bug, a bug I can't find in JIRA, or a feature? Seems to be fixable by:
scala> (Some(Wuffles): Option[Wuffles.type]) match { case Some(wuffles: Wuffles.type) => println("Woof"); case _ => println("Meow") }Woof


Chris
Chris Marshall
Joined: 2009-06-17,
User offline. Last seen 44 weeks 3 days ago.
RE: Failing to match on singleton type
Reported:  https://issues.scala-lang.org/browse/SI-5406 

From: oxbow_lakes@hotmail.com
To: scala-user@googlegroups.com
Subject: [scala-user] Failing to match on singleton type
Date: Tue, 24 Jan 2012 11:02:55 +0000

.ExternalClass .ecxhmmessage P {padding:0px;} .ExternalClass body.ecxhmmessage {font-size:10pt;font-family:Tahoma;} This is using 2.9.1 - I can't find any matching issues
scala> object Wufflesdefined module Wuffles
scala> (Some(Wuffles): Option[Wuffles.type]) match { case Some(Wuffles) => println("Woof"); case _ => println("Meow") }<console>:10: error: pattern type is incompatible with expected type; found   : object Wuffles required: Wuffles.type              (Some(Wuffles): Option[Wuffles.type]) match { case Some(Wuffles) => println("Woof"); case _ => println("Meow") }                                                                      ^

The complaint is seems to be that Wuffles is not of Wuffles.type
scala> Wufflesres10: Wuffles.type = Wuffles$@11dc088

OK. Is this a new bug, a bug I can't find in JIRA, or a feature? Seems to be fixable by:
scala> (Some(Wuffles): Option[Wuffles.type]) match { case Some(wuffles: Wuffles.type) => println("Woof"); case _ => println("Meow") }Woof


Chris
Alex Repain
Joined: 2010-07-27,
User offline. Last seen 1 year 31 weeks ago.
Re: Failing to match on singleton type

I have no preliminary knowledge in this, but here are some clues :

> object W
defined module W

> (Some(W): Option[_ >: W.type]) match {
> case Some(W) => "yo"
> case None => "no"
> }
res21: java.lang.String = yo

also :

> class A
defined class A

> object B extends A
defined module B

> (Some(B): Option[A]) match {case Some(B) => "yo"; case None => "no"}
res14: java.lang.String = yo

What I suspect is that B.type is an artificial type only meant for
reflection, and that doesn't appear in the type hierarchy of your
program, from the compiler point of view.
B.isInstanceOf[B.type] will yield true. Some compiler magic for
reflection purposes ? Maybe structural types are involved ?

Waiting for the big shots to answer here :).

Alex

2012/1/25, Chris Marshall :
>
> Reported:
> https://issues.scala-lang.org/browse/SI-5406
>
> From: oxbow_lakes@hotmail.com
> To: scala-user@googlegroups.com
> Subject: [scala-user] Failing to match on singleton type
> Date: Tue, 24 Jan 2012 11:02:55 +0000
>
>
>
>
>
>
>
> This is using 2.9.1 - I can't find any matching issues
> scala> object Wufflesdefined module Wuffles
> scala> (Some(Wuffles): Option[Wuffles.type]) match { case Some(Wuffles) =>
> println("Woof"); case _ => println("Meow") }:10: error: pattern
> type is incompatible with expected type; found : object Wuffles required:
> Wuffles.type (Some(Wuffles): Option[Wuffles.type]) match { case
> Some(Wuffles) => println("Woof"); case _ => println("Meow") }
> ^
> The complaint is seems to be that Wuffles is not of Wuffles.type
> scala> Wufflesres10: Wuffles.type = Wuffles$@11dc088
> OK. Is this a new bug, a bug I can't find in JIRA, or a feature? Seems to be
> fixable by:
> scala> (Some(Wuffles): Option[Wuffles.type]) match { case Some(wuffles:
> Wuffles.type) => println("Woof"); case _ => println("Meow") }Woof
>
> Chris

moors
Joined: 2010-10-06,
User offline. Last seen 36 weeks 4 days ago.
Re: Failing to match on singleton type
I don't have an answer, but the compiler indeed has two ways of referring to the singleton type that is an object typewe have TypeRef(pre, TheObjectSymbol, List()) and SingleType(pre, TheObjectSymbol)
this duplicity has caused trouble at several times in the past, but I can't really provide any insight into why there cannot only be one (I can't even seem to find a specific ticket about this in jira)

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland