Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scala interpreter has a problem with else in new line #1945

Closed
scabug opened this issue May 1, 2009 · 4 comments
Closed

Scala interpreter has a problem with else in new line #1945

scabug opened this issue May 1, 2009 · 4 comments
Assignees

Comments

@scabug
Copy link

scabug commented May 1, 2009

Open scala interpreter and load a file that contains

val half =
	if (n % 2 == 0) 
		n / 2 
	else 
		throw new RuntimeException("n must be even")

--> illegal start of definition

whereas

val half = if (n % 2 == 0) n / 2 else throw new RuntimeException("n must be even") 

works fine.

@scabug
Copy link
Author

scabug commented May 1, 2009

Imported From: https://issues.scala-lang.org/browse/SI-1945?orig=1
Reporter: Thomas (foofighter)

@scabug
Copy link
Author

scabug commented May 1, 2009

@paulp said (edited by @som-snytt on Aug 31, 2013 5:40:52 PM UTC):
Sorry, this is a limitation of the interpreter. val half = if (x) y is a valid statement all by itself, so when you press enter it is processed. Avoiding this would require forcing everyone to press enter twice all the time, which is no good.

You can do this:

val half = (
    if (n % 2 == 0)
        n / 2
    else
        throw new RuntimeException("n must be even")
)

@scabug scabug closed this as completed May 18, 2011
@scabug
Copy link
Author

scabug commented Aug 31, 2013

@som-snytt said:
One heuristic is that if line 2 fails with a syntax error, try again with line 1 + line 2.

This works exactly if line 1 is a prefix of the combined input that compiles.

You could capture more errors by retrying with typer errors, for example, postfix expressions that are currently handled by the leading-dot syntax:

> List("one"->2)
> toMap

but I'm guessing there are edge cases.

@scabug
Copy link
Author

scabug commented Sep 25, 2013

@som-snytt said:
Here is that PR: scala/scala#2989

Not reopening until consensus.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants