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

-1.toString

11 replies
dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
I'm curious about the error we get with this expression:   scala> -1.toString
<console>:1: error: ';' expected but '.' found.
       -1.toString
         ^   Given that "-1" is a valid literal -- according to Scala's EBNF -- and that "1.toString" works, this wouldn't seem invalid. Now, if "-" is getting treated as a prefix operator, I could understand an error message about the method unary_- not being a part of java.lang.String. But ";" expected just doesn't make sense, given that there is a valid resolution of it on Scala's EBNF. Furthermore, if we replace "1" by an identifier (of Int type), the error becomes the expected one.   Given that this error happens only for an integer literal preceded by the minus sign, I can only think of a conflict between resolving "-1" as a Literal, or a PrefixExpr with a literal for SimpleExpr, which is screwing with the backtrack.   Can anyone shed light on this?

--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: -1.toString

On Wed, Jun 03, 2009 at 03:53:38PM -0300, Daniel Sobral wrote:
> Given that "-1" is a valid literal -- according to Scala's EBNF -- and
> that "1.toString" works, this wouldn't seem invalid.

If -1 were the last literal in there you'd be on to something, but:

scala> -1.
res0: Double = -1.0

So ";" expected means it looks to mr. parser like you're jamming a
double and an identifier together for some mysterious reason.

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: -1.toString

On Wed, Jun 03, 2009 at 03:53:38PM -0300, Daniel Sobral wrote:
> scala> -1.toString
> :1: error: ';' expected but '.' found.
> -1.toString
> ^

OK, that last answer while superficially plausible had the disadvantage
of not being correct, since it could equally well be used to say that
1.toString should error.

I looked at the parser and this appears to be an artifact of special
handling for negated literals.

def literal(isNegated: Boolean): Tree = {
val isSymLit = in.token == SYMBOLLIT
val t = Literal {
in.token match {
case CHARLIT => Constant(in.charVal)
case INTLIT => Constant(in.intVal(isNegated).toInt)
case LONGLIT => Constant(in.intVal(isNegated))
case FLOATLIT => Constant(in.floatVal(isNegated).toFloat)
case DOUBLELIT => Constant(in.floatVal(isNegated))

...so I think "1." parses as a double and then since the preceding token
is MINUS it creates a Constant(-1.) which then chokes.

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: -1.toString

On Wed, Jun 3, 2009 at 5:04 PM, Paul Phillips wrote:
>
> On Wed, Jun 03, 2009 at 03:53:38PM -0300, Daniel Sobral wrote:
> > scala> -1.toString
> > :1: error: ';' expected but '.' found.
> >        -1.toString
> >          ^
>
> OK, that last answer while superficially plausible had the disadvantage
> of not being correct, since it could equally well be used to say that
> 1.toString should error.
>
> I looked at the parser and this appears to be an artifact of special
> handling for negated literals.
>
[...]
>
> ...so I think "1." parses as a double and then since the preceding token
> is MINUS it creates a Constant(-1.) which then chokes.

So... it does't follow the EBNF?

--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic
reviews: review by name, review by reference and review by value.

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: -1.toString

On Wed, Jun 03, 2009 at 05:51:57PM -0300, Daniel Sobral wrote:
> So... it does't follow the EBNF?

We prefer to call them specification bugs. There are quite a few. But
so far this does not look wrong to me -- the scanner says it's a double
and an identifier, parse error. You mentioned backtracking in your
original email which gives me the impression you think the scala parser
is doing things it is not doing.

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: -1.toString

It bothers me on two different grounds. First, because it complains
about receiving "." an expecting ";", which I can't reconcile with
anything in the language specification. There are other error messages
that wouldn't bother me. I would, in fact, expect the same error
message I get with "+1.toString".

And if it were interpreting that as double and an identifier, I would
expect the error to be receiving "toString" and expecting ";".
Furthermore "1.toString" does work, so interpreting "-1." as Double
but not "1." is definitely wrong, as it is incoherent and violates the
principle of least suprise.

So, that's one ground. The other ground for bothersomeness is that
"-1" seem to be a valid literal, as far as the especification goes
(1.3). At first I assumed that -1 was not a valid literal, but a
prefix operator and a literal, so that the language could be
simplified. But if "-1" is a valid literal, then "-1.toString" should,
by all means, be valid.

On Wed, Jun 3, 2009 at 6:17 PM, Paul Phillips wrote:
> On Wed, Jun 03, 2009 at 05:51:57PM -0300, Daniel Sobral wrote:
>> So... it does't follow the EBNF?
>
> We prefer to call them specification bugs.  There are quite a few.  But
> so far this does not look wrong to me -- the scanner says it's a double
> and an identifier, parse error.  You mentioned backtracking in your
> original email which gives me the impression you think the scala parser
> is doing things it is not doing.
>
> --
> Paul Phillips      | A Sunday school is a prison in which children do
> Moral Alien        | penance for the evil conscience of their parents.
> Empiricist         |     -- H. L. Mencken
> all hip pupils!    |----------* http://www.improving.org/paulp/ *----------
>

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: -1.toString
In Sweden we use commas for decimal separation:

1,0.toString

looks better.

Sweden wins.

On Thu, Jun 4, 2009 at 12:12 AM, Daniel Sobral <dcsobral [at] gmail [dot] com> wrote:
It bothers me on two different grounds. First, because it complains
about receiving "." an expecting ";", which I can't reconcile with
anything in the language specification. There are other error messages
that wouldn't bother me. I would, in fact, expect the same error
message I get with "+1.toString".

And if it were interpreting that as double and an identifier, I would
expect the error to be receiving "toString" and expecting ";".
Furthermore "1.toString" does work, so interpreting "-1." as Double
but not "1." is definitely wrong, as it is incoherent and violates the
principle of least suprise.

So, that's one ground. The other ground for bothersomeness is that
"-1" seem to be a valid literal, as far as the especification goes
(1.3). At first I assumed that -1 was not a valid literal, but a
prefix operator and a literal, so that the language could be
simplified. But if "-1" is a valid literal, then "-1.toString" should,
by all means, be valid.

On Wed, Jun 3, 2009 at 6:17 PM, Paul Phillips <paulp [at] improving [dot] org> wrote:
> On Wed, Jun 03, 2009 at 05:51:57PM -0300, Daniel Sobral wrote:
>> So... it does't follow the EBNF?
>
> We prefer to call them specification bugs.  There are quite a few.  But
> so far this does not look wrong to me -- the scanner says it's a double
> and an identifier, parse error.  You mentioned backtracking in your
> original email which gives me the impression you think the scala parser
> is doing things it is not doing.
>
> --
> Paul Phillips      | A Sunday school is a prison in which children do
> Moral Alien        | penance for the evil conscience of their parents.
> Empiricist         |     -- H. L. Mencken
> all hip pupils!    |----------* http://www.improving.org/paulp/ *----------
>



--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic
reviews: review by name, review by reference and review by value.



--
Viktor Klang
Rockstar Developer
Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: -1.toString

That's heresy. Scala is a C-syntax language, and in C, 1,0.toString
would evaluate and ignore the 1, then give 0.toString! (ignoring that
0.toString is nonsense in C)

Don't try to change the feel of Scala.

Ahem.

2009/6/4 Viktor Klang :
> In Sweden we use commas for decimal separation:
>
> 1,0.toString
>
> looks better.
>
> Sweden wins.
>
> On Thu, Jun 4, 2009 at 12:12 AM, Daniel Sobral wrote:
>>
>> It bothers me on two different grounds. First, because it complains
>> about receiving "." an expecting ";", which I can't reconcile with
>> anything in the language specification. There are other error messages
>> that wouldn't bother me. I would, in fact, expect the same error
>> message I get with "+1.toString".
>>
>> And if it were interpreting that as double and an identifier, I would
>> expect the error to be receiving "toString" and expecting ";".
>> Furthermore "1.toString" does work, so interpreting "-1." as Double
>> but not "1." is definitely wrong, as it is incoherent and violates the
>> principle of least suprise.
>>
>> So, that's one ground. The other ground for bothersomeness is that
>> "-1" seem to be a valid literal, as far as the especification goes
>> (1.3). At first I assumed that -1 was not a valid literal, but a
>> prefix operator and a literal, so that the language could be
>> simplified. But if "-1" is a valid literal, then "-1.toString" should,
>> by all means, be valid.
>>
>> On Wed, Jun 3, 2009 at 6:17 PM, Paul Phillips wrote:
>> > On Wed, Jun 03, 2009 at 05:51:57PM -0300, Daniel Sobral wrote:
>> >> So... it does't follow the EBNF?
>> >
>> > We prefer to call them specification bugs.  There are quite a few.  But
>> > so far this does not look wrong to me -- the scanner says it's a double
>> > and an identifier, parse error.  You mentioned backtracking in your
>> > original email which gives me the impression you think the scala parser
>> > is doing things it is not doing.
>> >
>> > --
>> > Paul Phillips      | A Sunday school is a prison in which children do
>> > Moral Alien        | penance for the evil conscience of their parents.
>> > Empiricist         |     -- H. L. Mencken
>> > all hip pupils!    |----------* http://www.improving.org/paulp/
>> > *----------
>> >
>>
>>
>>
>> --
>> Daniel C. Sobral
>>
>> Something I learned in academia: there are three kinds of academic
>> reviews: review by name, review by reference and review by value.
>
>
>
> --
> Viktor Klang
> Rockstar Developer
>

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: -1.toString


On Thu, Jun 4, 2009 at 10:23 AM, Ricky Clarkson <ricky [dot] clarkson [at] gmail [dot] com> wrote:
That's heresy.  Scala is a C-syntax language, and in C, 1,0.toString
would evaluate and ignore the 1, then give 0.toString! (ignoring that
0.toString is nonsense in C)

Why having syntax for "ignore what I just wrote"? I have both DEL and Backspace keys.... they work just fine...
 


Don't try to change the feel of Scala.

Ahem.


So basically you religionized Scala now, with "Ahem" instead of "Amen"?

I'm confused...
...and hungry.
 


2009/6/4 Viktor Klang <viktor [dot] klang [at] gmail [dot] com>:
> In Sweden we use commas for decimal separation:
>
> 1,0.toString
>
> looks better.
>
> Sweden wins.
>
> On Thu, Jun 4, 2009 at 12:12 AM, Daniel Sobral <dcsobral [at] gmail [dot] com> wrote:
>>
>> It bothers me on two different grounds. First, because it complains
>> about receiving "." an expecting ";", which I can't reconcile with
>> anything in the language specification. There are other error messages
>> that wouldn't bother me. I would, in fact, expect the same error
>> message I get with "+1.toString".
>>
>> And if it were interpreting that as double and an identifier, I would
>> expect the error to be receiving "toString" and expecting ";".
>> Furthermore "1.toString" does work, so interpreting "-1." as Double
>> but not "1." is definitely wrong, as it is incoherent and violates the
>> principle of least suprise.
>>
>> So, that's one ground. The other ground for bothersomeness is that
>> "-1" seem to be a valid literal, as far as the especification goes
>> (1.3). At first I assumed that -1 was not a valid literal, but a
>> prefix operator and a literal, so that the language could be
>> simplified. But if "-1" is a valid literal, then "-1.toString" should,
>> by all means, be valid.
>>
>> On Wed, Jun 3, 2009 at 6:17 PM, Paul Phillips <paulp [at] improving [dot] org> wrote:
>> > On Wed, Jun 03, 2009 at 05:51:57PM -0300, Daniel Sobral wrote:
>> >> So... it does't follow the EBNF?
>> >
>> > We prefer to call them specification bugs.  There are quite a few.  But
>> > so far this does not look wrong to me -- the scanner says it's a double
>> > and an identifier, parse error.  You mentioned backtracking in your
>> > original email which gives me the impression you think the scala parser
>> > is doing things it is not doing.
>> >
>> > --
>> > Paul Phillips      | A Sunday school is a prison in which children do
>> > Moral Alien        | penance for the evil conscience of their parents.
>> > Empiricist         |     -- H. L. Mencken
>> > all hip pupils!    |----------* http://www.improving.org/paulp/
>> > *----------
>> >
>>
>>
>>
>> --
>> Daniel C. Sobral
>>
>> Something I learned in academia: there are three kinds of academic
>> reviews: review by name, review by reference and review by value.
>
>
>
> --
> Viktor Klang
> Rockstar Developer
>



--
Viktor Klang
Rockstar Developer
Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: -1.toString

No, you have it all wrong.

1.0.toString has, at least, the type (Fractional (b -> c)) => a -> c

Ricky Clarkson wrote:
> That's heresy. Scala is a C-syntax language, and in C, 1,0.toString
> would evaluate and ignore the 1, then give 0.toString! (ignoring that
> 0.toString is nonsense in C)
>
> Don't try to change the feel of Scala.
>
> Ahem.
>
> 2009/6/4 Viktor Klang :
>
>> In Sweden we use commas for decimal separation:
>>
>> 1,0.toString
>>
>> looks better.
>>
>> Sweden wins.
>>
>> On Thu, Jun 4, 2009 at 12:12 AM, Daniel Sobral wrote:
>>
>>> It bothers me on two different grounds. First, because it complains
>>> about receiving "." an expecting ";", which I can't reconcile with
>>> anything in the language specification. There are other error messages
>>> that wouldn't bother me. I would, in fact, expect the same error
>>> message I get with "+1.toString".
>>>
>>> And if it were interpreting that as double and an identifier, I would
>>> expect the error to be receiving "toString" and expecting ";".
>>> Furthermore "1.toString" does work, so interpreting "-1." as Double
>>> but not "1." is definitely wrong, as it is incoherent and violates the
>>> principle of least suprise.
>>>
>>> So, that's one ground. The other ground for bothersomeness is that
>>> "-1" seem to be a valid literal, as far as the especification goes
>>> (1.3). At first I assumed that -1 was not a valid literal, but a
>>> prefix operator and a literal, so that the language could be
>>> simplified. But if "-1" is a valid literal, then "-1.toString" should,
>>> by all means, be valid.
>>>
>>> On Wed, Jun 3, 2009 at 6:17 PM, Paul Phillips wrote:
>>>
>>>> On Wed, Jun 03, 2009 at 05:51:57PM -0300, Daniel Sobral wrote:
>>>>
>>>>> So... it does't follow the EBNF?
>>>>>
>>>> We prefer to call them specification bugs. There are quite a few. But
>>>> so far this does not look wrong to me -- the scanner says it's a double
>>>> and an identifier, parse error. You mentioned backtracking in your
>>>> original email which gives me the impression you think the scala parser
>>>> is doing things it is not doing.
>>>>
>>>> --
>>>> Paul Phillips | A Sunday school is a prison in which children do
>>>> Moral Alien | penance for the evil conscience of their parents.
>>>> Empiricist | -- H. L. Mencken
>>>> all hip pupils! |----------* http://www.improving.org/paulp/
>>>> *----------
>>>>
>>>>
>>>
>>> --
>>> Daniel C. Sobral
>>>
>>> Something I learned in academia: there are three kinds of academic
>>> reviews: review by name, review by reference and review by value.
>>>
>>
>> --
>> Viktor Klang
>> Rockstar Developer
>>
>>
>
>

ounos
Joined: 2008-12-29,
User offline. Last seen 3 years 44 weeks ago.
Re: -1.toString
Ah, the beauty of fractional programming. 

2009/6/4 Tony Morris <tonymorris [at] gmail [dot] com>
No, you have it all wrong.

1.0.toString has, at least, the type (Fractional (b -> c)) => a -> c


Ricky Clarkson wrote:
> That's heresy.  Scala is a C-syntax language, and in C, 1,0.toString
> would evaluate and ignore the 1, then give 0.toString! (ignoring that
> 0.toString is nonsense in C)
>
> Don't try to change the feel of Scala.
>
> Ahem.
>
> 2009/6/4 Viktor Klang <viktor [dot] klang [at] gmail [dot] com>:
>
>> In Sweden we use commas for decimal separation:
>>
>> 1,0.toString
>>
>> looks better.
>>
>> Sweden wins.
>>
>> On Thu, Jun 4, 2009 at 12:12 AM, Daniel Sobral <dcsobral [at] gmail [dot] com> wrote:
>>
>>> It bothers me on two different grounds. First, because it complains
>>> about receiving "." an expecting ";", which I can't reconcile with
>>> anything in the language specification. There are other error messages
>>> that wouldn't bother me. I would, in fact, expect the same error
>>> message I get with "+1.toString".
>>>
>>> And if it were interpreting that as double and an identifier, I would
>>> expect the error to be receiving "toString" and expecting ";".
>>> Furthermore "1.toString" does work, so interpreting "-1." as Double
>>> but not "1." is definitely wrong, as it is incoherent and violates the
>>> principle of least suprise.
>>>
>>> So, that's one ground. The other ground for bothersomeness is that
>>> "-1" seem to be a valid literal, as far as the especification goes
>>> (1.3). At first I assumed that -1 was not a valid literal, but a
>>> prefix operator and a literal, so that the language could be
>>> simplified. But if "-1" is a valid literal, then "-1.toString" should,
>>> by all means, be valid.
>>>
>>> On Wed, Jun 3, 2009 at 6:17 PM, Paul Phillips <paulp [at] improving [dot] org> wrote:
>>>
>>>> On Wed, Jun 03, 2009 at 05:51:57PM -0300, Daniel Sobral wrote:
>>>>
>>>>> So... it does't follow the EBNF?
>>>>>
>>>> We prefer to call them specification bugs.  There are quite a few.  But
>>>> so far this does not look wrong to me -- the scanner says it's a double
>>>> and an identifier, parse error.  You mentioned backtracking in your
>>>> original email which gives me the impression you think the scala parser
>>>> is doing things it is not doing.
>>>>
>>>> --
>>>> Paul Phillips      | A Sunday school is a prison in which children do
>>>> Moral Alien        | penance for the evil conscience of their parents.
>>>> Empiricist         |     -- H. L. Mencken
>>>> all hip pupils!    |----------* http://www.improving.org/paulp/
>>>> *----------
>>>>
>>>>
>>>
>>> --
>>> Daniel C. Sobral
>>>
>>> Something I learned in academia: there are three kinds of academic
>>> reviews: review by name, review by reference and review by value.
>>>
>>
>> --
>> Viktor Klang
>> Rockstar Developer
>>
>>
>
>

--
Tony Morris
http://tmorris.net/



Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: -1.toString

Viktor,

I'm sure you're aware, but in C, that syntax is useful for invoking a
side effect, e.g.: for (int x = 0, y = 1; x < 100; x += x + y, y++)

I was parodying the nonsense around "the feel of Java" regarding not
adding lambdas to the language.

2009/6/4 Viktor Klang :
>
>
> On Thu, Jun 4, 2009 at 10:23 AM, Ricky Clarkson
> wrote:
>>
>> That's heresy.  Scala is a C-syntax language, and in C, 1,0.toString
>> would evaluate and ignore the 1, then give 0.toString! (ignoring that
>> 0.toString is nonsense in C)
>
> Why having syntax for "ignore what I just wrote"? I have both DEL and
> Backspace keys.... they work just fine...
>
>>
>> Don't try to change the feel of Scala.
>>
>> Ahem.
>
> So basically you religionized Scala now, with "Ahem" instead of "Amen"?
>
> I'm confused...
> ...and hungry.
>
>>
>> 2009/6/4 Viktor Klang :
>> > In Sweden we use commas for decimal separation:
>> >
>> > 1,0.toString
>> >
>> > looks better.
>> >
>> > Sweden wins.
>> >
>> > On Thu, Jun 4, 2009 at 12:12 AM, Daniel Sobral
>> > wrote:
>> >>
>> >> It bothers me on two different grounds. First, because it complains
>> >> about receiving "." an expecting ";", which I can't reconcile with
>> >> anything in the language specification. There are other error messages
>> >> that wouldn't bother me. I would, in fact, expect the same error
>> >> message I get with "+1.toString".
>> >>
>> >> And if it were interpreting that as double and an identifier, I would
>> >> expect the error to be receiving "toString" and expecting ";".
>> >> Furthermore "1.toString" does work, so interpreting "-1." as Double
>> >> but not "1." is definitely wrong, as it is incoherent and violates the
>> >> principle of least suprise.
>> >>
>> >> So, that's one ground. The other ground for bothersomeness is that
>> >> "-1" seem to be a valid literal, as far as the especification goes
>> >> (1.3). At first I assumed that -1 was not a valid literal, but a
>> >> prefix operator and a literal, so that the language could be
>> >> simplified. But if "-1" is a valid literal, then "-1.toString" should,
>> >> by all means, be valid.
>> >>
>> >> On Wed, Jun 3, 2009 at 6:17 PM, Paul Phillips
>> >> wrote:
>> >> > On Wed, Jun 03, 2009 at 05:51:57PM -0300, Daniel Sobral wrote:
>> >> >> So... it does't follow the EBNF?
>> >> >
>> >> > We prefer to call them specification bugs.  There are quite a few.
>> >> >  But
>> >> > so far this does not look wrong to me -- the scanner says it's a
>> >> > double
>> >> > and an identifier, parse error.  You mentioned backtracking in your
>> >> > original email which gives me the impression you think the scala
>> >> > parser
>> >> > is doing things it is not doing.
>> >> >
>> >> > --
>> >> > Paul Phillips      | A Sunday school is a prison in which children do
>> >> > Moral Alien        | penance for the evil conscience of their
>> >> > parents.
>> >> > Empiricist         |     -- H. L. Mencken
>> >> > all hip pupils!    |----------* http://www.improving.org/paulp/
>> >> > *----------
>> >> >
>> >>
>> >>
>> >>
>> >> --
>> >> Daniel C. Sobral
>> >>
>> >> Something I learned in academia: there are three kinds of academic
>> >> reviews: review by name, review by reference and review by value.
>> >
>> >
>> >
>> > --
>> > Viktor Klang
>> > Rockstar Developer
>> >
>
>
>
> --
> Viktor Klang
> Rockstar Developer
>

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