Scala compiler says unreachable code, why?

I'm new to Scala... Here's the code:

def ack2(m: BigInt, n: BigInt): BigInt = { val z = BigInt(0) (m,n) match { case (z,_) => n+1 case (_,z) => ack2(m-1,1) // Compiler says unreachable code on the paren of ack2( case _ => ack2(m-1, ack2(m, n-1)) // Compiler says unreachable code on the paren of ack2( } }

I'm trying to understand that... why is it giving that error?

Note: I'm using Scala Eclipse Plugin 2.8.0.r21376-b20100408034031 ch.epfl.lamp.sdt.feature.group

--------------Solutions-------------

The z inside the pattern match does not refer to the z you declared outside, it introduces a new variable binding. So the first case will match every possible pair (binding z to the first element of the pair and discarding the second) and the other cases will never be reached.

If you replace z in the pattern with

`z`

it will refer to the existing z and not introduce a new binding, so it will work as you intend. You can also rename z to Z if you don't like the syntax with backticks.

Category:scala Time:2010-04-13 Views:1

Related post

  • Generating Scala code trees from a Scala compiler plugin 2010-02-12

    There are a few resources on the web that are instructive in writing Scala compiler plugins that pattern-match against the code, but these don't help in generating code (constructing symbol trees). Where should I start to figure out how to do this? (

  • Does unreachable code within #ifdef and #endif get removed during compile/link time? 2011-03-21

    Does unreachable code within #ifdef and #endif get removed during compile/link time or kept in the executable even though it can not be reached? #ifdef DEBUG // some debug operations #endif Is the debug operation code still kept after compilation wit

  • Unreachable code compiler error 2012-02-14

    The following code gives an unreachable statement compiler error public static void main(String[] args) { return; System.out.println("unreachable"); } Sometimes for testing purposes a want to prevent a method from being called, so a quick way to do i

  • Compile time errors and unreachable code 2012-02-23

    Ok, consider the following code: private const int THRESHHOLD = 2; static void Main(string[] args) { string hello; if (THRESHHOLD > 1) return; Console.WriteLine(hello); } Suprisingly this code does not throw a "Use of unassigned local variable 'he

  • VB Unreachable code error/warning 2009-03-31

    I've been mostly working with VB.Net for over a year and just noticed this Am I going insane, or does VB.Net NOT have an "Unreachable code" warning? The following compiles quite happily with nary a warning or error, even though there is a return betw

  • PLW-06002 unreachable code when using NULL; 2009-08-28

    I occasionally do something like.... IF very-likely-condition THEN NULL; ELSE <<code to deal with the unlikely condition>> END IF; Which gives a PLW-06002 unreachable code warning from the PL/SQL compiler on the NULL line atfer the IF. No

  • Is there a way to get VS2008 to stop warning me about unreachable code? 2009-12-18

    I have a few config options in my application along the lines of const bool ExecuteThis=true; const bool ExecuteThat=false; and then code that uses it like if(ExecuteThis){ DoThis(); } if(ExecuteThat){ DoThat(); } //unreachable code warning here The

  • Unreachable code error vs. dead code warning in Java under Eclipse? 2010-01-26

    Does anyone know why: public void foo() { System.out.println("Hello"); return; System.out.println("World!"); } Would be reported as an "unreachable error" under Eclipse, but public void foo() { System.out.println("Hello"); if(true) return; System.out

  • how to use scala internals to compile source faster than fsc (fast scala compiler) 2010-02-14

    fsc (fast scala compiler) is faster than scalac. but during TDD cycles i consume 3 seconds to compile sources over less than 1s to run my tests.. suggestion to reduce compile time near 0? obviously, buy a faster computer is not an answer :) i mean us

  • Unreachable code: error or warning? 2010-02-17

    This is a language design question: Do you think unreachable code (in programming languages in general) should raise a warning (i.e. "report problem and compile anyway") or an error ("refuse to compile")? Personally I strongly feel it should be an er

  • Unreachable code detected in case statement 2010-04-15

    I have a code: protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { switch (keyData) { case Keys.Alt|Keys.D1: if (this._condition1) { return true; } else { return base.ProcessCmdKey(ref msg, keyData); } break; case Keys.Control |Key

  • Why is there unreachable code here? 2010-04-25

    I am writing a c# app and want to output error messages to either the console or a messagebox (Depending on the app type: enum AppTypeChoice { Console, Windows } ), and also control wether the app keeps running or not ( bool StopOnError ). I came up

  • Is there an alternative to suppressing warnings for unreachable code in the xtree? 2010-07-12

    When using the std::map with types that use trivial, non-throwing, copy constructors, a compiler warning/error is thrown (warning level 4, release mode) for unreachable code in xtree. This is because the std::map has a try-catch in it that helps main

  • Java compile speed vs Scala compile speed 2010-08-16

    I've been programming in Scala for a while and I like it but one thing I'm annoyed by is the time it takes to compile programs. It's seems like a small thing but with Java I could make small changes to my program, click the run button in netbeans, an

  • Does SBT use the Fast Scala Compiler (fsc)? 2010-09-02

    Does SBT make use of fsc? For test purposes I am compiling a 500-line program on a fairly slow Ubuntu machine (Atom N270). Three successive compile times were 77s, 66s, and 66s. I then compiled the file with fsc from the command line. Now my times we

  • Why does the Scala compiler prefer to infer an argument of value null as Array[Char] instead of Object? 2011-01-15

    Consider these methods from java.lang.String /** * Returns the string representation of the <code>Object</code> argument. * * @param obj an <code>Object</code>. * @return if the argument is <code>null</code>, then

  • How to avoid warning of Unreachable code 2011-01-19

    I have the below string currency = string.Empty; Regex r = new Regex(@"~(\w*[a-zA-Z0-9$£~%]+)", RegexOptions.IgnoreCase | RegexOptions.Compiled); for (Match m = r.Match(expression); m.Success; m = m.NextMatch()) { currency = (m.Groups[1].Value); brea

  • Scala: compilation error when declaring continuation of type Any => Nothing 2011-03-18

    This code gives compilation error: import scala.util.continuations._ object CTest { def loop: Nothing = reset { shift {c: (Unit => Nothing) => c()} loop } def main(argv: Array[String]) {loop} } Error message: error: type mismatch; found : ((Uni

  • Java: How to @SuppressWarnings unreachable code? 2011-05-17

    Sometimes when you are debugging, you have unreachable code fragment. Is there anyway to suppress the warning? --------------Solutions------------- The only way to do this on any compiler is @SupressWarnings("all"). If you're using Eclipse, try @Supp

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.428 (s). 13 q(s)