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

Puzzling Case of Unreachable Code

5 replies
Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.

Hi,

I cannot see why the following code is deemed unreachable by the Scala compiler:

-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
object TraversalVerb
extends Enumeration
{
val enter = Value
val ahead = Value
val exit = Value
}

case class TraversalAction(val verb: TraversalVerb.Value, val node: ExpressionCursor)
-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-

-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
Traversals.navigateExpression(formula) {
action: TraversalAction =>
action.verb match {
case enter => render('(')

case ahead => { // <-- Line 238
if (latestVerb == ahead)
render(' ')

render(action.node.at)
}

case exit => render(')') // <-- Line 246
}
latestVerb = action.verb
}
-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-

-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
[scalac] /dar/rho/src/rho/io/FOL_IO.scala:238: error: unreachable code
[scalac] case ahead => {
[scalac] ^
[scalac] /dar/rho/src/rho/io/FOL_IO.scala:246: error: unreachable code
[scalac] render(')')
[scalac] ^
[scalac] two errors found
-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-

Can someone explain why the compiler deems those statements unreachable?

Randall Schulz

Christian Szegedy
Joined: 2009-02-08,
User offline. Last seen 42 years 45 weeks ago.
Re: Puzzling Case of Unreachable Code

Yoiu should capitalize enter, ahead and exit. Otherwise they are taken
for a a variables, resulting in irrefutable match.

On 3/23/09, Randall R Schulz wrote:
> Hi,
>
> I cannot see why the following code is deemed unreachable by the Scala compiler:
>
> -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
> object TraversalVerb
> extends Enumeration
> {
> val enter = Value
> val ahead = Value
> val exit = Value
> }
>
> case class TraversalAction(val verb: TraversalVerb.Value, val node: ExpressionCursor)
> -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
>
> -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
> Traversals.navigateExpression(formula) {
> action: TraversalAction =>
> action.verb match {
> case enter => render('(')
>
> case ahead => { // <-- Line 238
> if (latestVerb == ahead)
> render(' ')
>
> render(action.node.at)
> }
>
> case exit => render(')') // <-- Line 246
> }
> latestVerb = action.verb
> }
> -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
>
> -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
> [scalac] /dar/rho/src/rho/io/FOL_IO.scala:238: error: unreachable code
> [scalac] case ahead => {
> [scalac] ^
> [scalac] /dar/rho/src/rho/io/FOL_IO.scala:246: error: unreachable code
> [scalac] render(')')
> [scalac] ^
> [scalac] two errors found
> -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
>
>
> Can someone explain why the compiler deems those statements unreachable?
>
>
>
> Randall Schulz
>

Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.
Re: Puzzling Case of Unreachable Code

On Monday March 23 2009, Christian Szegedy wrote:
> You should capitalize enter, ahead and exit. Otherwise they are
> taken for a a variables, resulting in irrefutable match.
>
> ...

Really? Wow.

I started to ask about enumerated value names on #scala yesterday, but
then decided not to pester folks with another trifling question.

So... The leading alphabetic case of identifiers is actually significant
to the compiler? Shades of Prolog...

Randall Schulz

Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Puzzling Case of Unreachable Code
The alternative is to quote it with back quotes - `enter`.

On Mon, Mar 23, 2009 at 1:55 PM, Randall R Schulz <rschulz@sonic.net> wrote:
On Monday March 23 2009, Christian Szegedy wrote:
> You should capitalize enter, ahead and exit. Otherwise they are
> taken for a a variables, resulting in irrefutable match.
>
> ...

Really? Wow.

I started to ask about enumerated value names on #scala yesterday, but
then decided not to pester folks with another trifling question.

So... The leading alphabetic case of identifiers is actually significant
to the compiler? Shades of Prolog...


Randall Schulz

ounos
Joined: 2008-12-29,
User offline. Last seen 3 years 44 weeks ago.
Re: Puzzling Case of Unreachable Code
Can someone explain why this was chosen so? Where could it be useful, or necessary? I only see it till now as confusing. 

2009/3/23 Naftoli Gugenheim <naftoligug@gmail.com>
The alternative is to quote it with back quotes - `enter`.

On Mon, Mar 23, 2009 at 1:55 PM, Randall R Schulz <rschulz@sonic.net> wrote:
On Monday March 23 2009, Christian Szegedy wrote:
> You should capitalize enter, ahead and exit. Otherwise they are
> taken for a a variables, resulting in irrefutable match.
>
> ...

Really? Wow.

I started to ask about enumerated value names on #scala yesterday, but
then decided not to pester folks with another trifling question.

So... The leading alphabetic case of identifiers is actually significant
to the compiler? Shades of Prolog...


Randall Schulz


Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Puzzling Case of Unreachable Code
The other use of back quotes is to refer to an identifier that is a Scala keyword, like Thread.yield. (JavaFX uses <<>> IIRC). So it forces a word to be interpreted as an identifier. Similarly, in pattern matching, lowercase is assumed to be a placeholder to capture a value, not to refer to an existing identifier, unless you tell the compiler that it is.

On Mon, Mar 23, 2009 at 5:21 PM, Jim Andreou <jim.andreou@gmail.com> wrote:
Can someone explain why this was chosen so? Where could it be useful, or necessary? I only see it till now as confusing. 

2009/3/23 Naftoli Gugenheim <naftoligug@gmail.com>
The alternative is to quote it with back quotes - `enter`.

On Mon, Mar 23, 2009 at 1:55 PM, Randall R Schulz <rschulz@sonic.net> wrote:
On Monday March 23 2009, Christian Szegedy wrote:
> You should capitalize enter, ahead and exit. Otherwise they are
> taken for a a variables, resulting in irrefutable match.
>
> ...

Really? Wow.

I started to ask about enumerated value names on #scala yesterday, but
then decided not to pester folks with another trifling question.

So... The leading alphabetic case of identifiers is actually significant
to the compiler? Shades of Prolog...


Randall Schulz



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