Fun with Number readFrom: What should we do ?

Francisco Garau francisco.garau at gmail.com
Thu Apr 27 23:21:54 UTC 2006


I totally agree with Diego.

Moreover, I think that each message should do the parsing on exactly one 
format. Something like this:

    (SqNumberParser on: '1.23') number
    (SqNumberParser on: '1.23e4) scientificNotation; number
    (SqNumberParser on: '2r1111') base2Notation; number
    (SqNumberParser on: '') cvsNotation; number "would answer 0"

And then we can add a top level method to try several alternatives (in a 
specific order)

    SqNumberParser>>parse
        try scientificNotation ifTrue: [^got it]
        try xxxNotation ifTrue: [^got it]
        try base2Notation ifTrue: [^got it]
        try base16Notation ifTrue: [^got it]
        else ParseException signal

This would be both flexible and generic. Those who know precisely the string 
format of an expected number, can use the specific parse message. And if you 
don't know the string format, just use the generic parse message.

String>>sqNumber
    ^(SqNumberParser on: self) parse; number

By using the Sq prefix, we would avoid clashing with existing methods. The 
code that is relying on the current funny behaviour would remain unaffected.

Cheers,
Francisco


----- Original Message ----- 
From: "Diego Fernandez" <diegof79 at gmail.com>
To: <ncellier at ifrance.com>; "The general-purpose Squeak developers list" 
<squeak-dev at lists.squeakfoundation.org>
Sent: Thursday, April 27, 2006 2:59 PM
Subject: Re: Fun with Number readFrom: What should we do ?


> My proposition is:
>
> Number>>readFrom: aStringOrStream
>     ^self readFrom: aStringOrStream ifFail: [^self error: 'cannot read a
> number']
>
> If nil is what you want, you will have to write:
>     value := Number readFrom: aStream ifFail: [nil].
> A little longer than your proposition:
>     value := Number readfrom: aStream.

The mine is:
Number>>readFrom: aStringOrStream
      ^NumberParser new parse: aStringOrStream

NumberParser>>parse: aStringOrStream
       "on failure"
        NumberParsingException signal
        "or just ParseException"

I think that #readFrom: must be an extension of Number.
So if you want more control of the parsing you can configure an
instance of NumberParser.
(may be you can have a FortranNumberParser :)).




More information about the Squeak-dev mailing list