- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Where does @unchecked go on an anonymous partial function?
Mon, 2011-11-07, 23:47
I have a handy function that takes a partial function as an argument:
def handyFunction [T] (f: PartialFunction [X, T]): T
where X is the root of some algebraic datatype, that is X is a sealed abstract class with several subclasses like A, B, C, D.
When I use handyFunction I get the "match not exhaustive!" warning:
handyFunction { case v: A => 1 case v: B => 2} >>> match not exhaustive!
But I've written handyFunction to specifically handle (handily handle!) the undefined cases, so in many occurrences the partial function is not exhaustive. The problem is, I can't figure out where the @unchecked goes. How do I make the warning go away?
I could do something like:
handyFunction { v => (v: @unchecked) match { case _: A => 1 case _: B => 2 }}
but then I'm no longer giving handyFunction a partial function, and handyFunction can no longer use isDefinedAt internally. Furthermore, this option needs syntactic muck.
I'm guessing there's a compiler flag to disable the warning, but I'd like to avoid the nuclear option.
I'm also guessing this is a not uncommon question, but I failed to dig up a relevant answer.
Thanks,Topher.
def handyFunction [T] (f: PartialFunction [X, T]): T
where X is the root of some algebraic datatype, that is X is a sealed abstract class with several subclasses like A, B, C, D.
When I use handyFunction I get the "match not exhaustive!" warning:
handyFunction { case v: A => 1 case v: B => 2} >>> match not exhaustive!
But I've written handyFunction to specifically handle (handily handle!) the undefined cases, so in many occurrences the partial function is not exhaustive. The problem is, I can't figure out where the @unchecked goes. How do I make the warning go away?
I could do something like:
handyFunction { v => (v: @unchecked) match { case _: A => 1 case _: B => 2 }}
but then I'm no longer giving handyFunction a partial function, and handyFunction can no longer use isDefinedAt internally. Furthermore, this option needs syntactic muck.
I'm guessing there's a compiler flag to disable the warning, but I'd like to avoid the nuclear option.
I'm also guessing this is a not uncommon question, but I failed to dig up a relevant answer.
Thanks,Topher.
Tue, 2011-11-08, 12:37
#2
Re: Where does @unchecked go on an anonymous partial function?
On 7 November 2011 22:47, Topher <topher.the.geek@gmail.com> wrote:
I have a handy function that takes a partial function as an argument:...
But I've written handyFunction to specifically handle (handily handle!) the undefined cases, so in many occurrences the partial function is not exhaustive. The problem is, I can't figure out where the @unchecked goes. How do I make the warning go away?
Is it possible to factor out the fall-through cases in handyFunction in terms of some sub-set of A, B,C, D? If so, you can say that it chews on some Y <: X that captures this sub-set. Alternatively, you can use a typeclass as 'proof' of this subset and pass it in as an implicit. I've used both these approaches when writing fairly general-purpose AST-rewriting functions (e.g. de-morgans) that need to be parameterized with specific types (e.g. the basis propositions that are being anded/ored/notted).
Matthew
Thanks,Topher.
--
Dr Matthew PocockIntegrative Bioinformatics Group, School of Computing Science, Newcastle Universitymailto: turingatemyhamster@gmail.com gchat: turingatemyhamster@gmail.commsn: matthew_pocock@yahoo.co.uk irc.freenode.net: drdozerskype: matthew.pococktel: (0191) 2566550mob: +447535664143
From the Odersky book (Programming in Scala 2nd Edition, section 15.5) you seem to have 2 choices here. Either what you've already done with the @unchecked above, or add an extra catch-all case and throw a RuntimeException. Personally I'd lean towards the Exception throw as its a nice ugly indicator when your assumptions change down the line and you forget to update that function.