David J. Biesack | 15 Oct 22:35
Favicon

RegexParsers implicits confusing my code


I'm trying to define a regex parser that ignores case, so "do" and "Do" and "DO" all match.

Why does the following object compile:

    Process inferior-scala finished
    Welcome to Scala version 2.7.2.RC3 (Java HotSpot(TM) Client VM, Java 1.6.0).
    Type in expressions to have them evaluated.
    Type :help for more information.
    scala> object MyParser {

          type Elem = Char

          def ire(s:String) : String = s.map((c:Char) => "(" + c.toUpperCase + "|" + c.toLowerCase + ")").reduceLeft(_+_)
          def tokeni(s:String) = ire(s).r

          val DO                        = tokeni("DO")
          val WHILE                     = tokeni("WHILE")
        }
    defined module MyParser,

This works as I expect:

    scala> MyParser.DO
    res0: scala.util.matching.Regex = (D|d)(O|o)

    scala> MyParser.WHILE
    res1: scala.util.matching.Regex = (W|w)(H|h)(I|i)(L|l)(E|e)

However, the following fails when I add RegexParsers:
(Continue reading)

Mark Harrah | 16 Oct 00:03
Favicon

Re: RegexParsers implicits confusing my code

On Wednesday 15 October 2008 16:38, David J. Biesack wrote:
> 
> I'm trying to define a regex parser that ignores case, so "do" and "Do" 
and "DO" all match.
> 
> Why does the following object compile:
> 
>     Process inferior-scala finished
>     Welcome to Scala version 2.7.2.RC3 (Java HotSpot(TM) Client VM, Java 
1.6.0).
>     Type in expressions to have them evaluated.
>     Type :help for more information.
>     scala> object MyParser {
> 
>           type Elem = Char
> 
>           def ire(s:String) : String = s.map((c:Char) => "(" + c.toUpperCase 
+ "|" + c.toLowerCase + ")").reduceLeft(_+_)
>           def tokeni(s:String) = ire(s).r
> 
>           val DO                        = tokeni("DO")
>           val WHILE                     = tokeni("WHILE")
>         }
>     defined module MyParser,
> 
> 
> This works as I expect:
> 
>     scala> MyParser.DO
>     res0: scala.util.matching.Regex = (D|d)(O|o)
(Continue reading)

David J. Biesack | 16 Oct 20:32
Favicon

Re: RegexParsers implicits confusing my code


Thanks, Mark - very useful. I added this to the Language FAQ on the Scala wiki :

http://scala.sygneca.com/faqs/language#how-do-i-resolve-a-implicit-conversions-are-not-applicable-compiler-error-message

Perhaps it is not *frequently* asked, but I did want to capture it.

(Perhaps there should be a separate troubleshooting FAQ; I'm not familiar enough with this wiki to create
it.) 

BTW, the scaladoc for scala.runtime.RichString should indicate how one creates one when it is necessary;
i.e. link to Predef.stringWrapper. See http://lampsvn.epfl.ch/trac/scala/ticket/1425

djb

--

-- 
David J. Biesack     SAS Institute Inc.
(919) 531-7771       SAS Campus Drive
http://www.sas.com   Cary, NC 27513

Mark Harrah | 17 Oct 02:16
Favicon

Re: RegexParsers implicits confusing my code

On Thursday 16 October 2008, David J. Biesack wrote:
> Thanks, Mark - very useful. I added this to the Language FAQ on the Scala
> wiki :
>
> http://scala.sygneca.com/faqs/language#how-do-i-resolve-a-implicit-conversi
>ons-are-not-applicable-compiler-error-message
>
> Perhaps it is not *frequently* asked, but I did want to capture it.
>
> (Perhaps there should be a separate troubleshooting FAQ; I'm not familiar
> enough with this wiki to create it.)
>
> BTW, the scaladoc for scala.runtime.RichString should indicate how one
> creates one when it is necessary; i.e. link to Predef.stringWrapper. See
> http://lampsvn.epfl.ch/trac/scala/ticket/1425
>

I should have mentioned option #4:

(new RichString(s)).map ...

> djb

David MacIver | 17 Oct 12:00

Re: RegexParsers implicits confusing my code

On Fri, Oct 17, 2008 at 1:16 AM, Mark Harrah <harrah <at> bu.edu> wrote:
> On Thursday 16 October 2008, David J. Biesack wrote:
>> Thanks, Mark - very useful. I added this to the Language FAQ on the Scala
>> wiki :
>>
>> http://scala.sygneca.com/faqs/language#how-do-i-resolve-a-implicit-conversi
>>ons-are-not-applicable-compiler-error-message
>>
>> Perhaps it is not *frequently* asked, but I did want to capture it.
>>
>> (Perhaps there should be a separate troubleshooting FAQ; I'm not familiar
>> enough with this wiki to create it.)
>>
>> BTW, the scaladoc for scala.runtime.RichString should indicate how one
>> creates one when it is necessary; i.e. link to Predef.stringWrapper. See
>> http://lampsvn.epfl.ch/trac/scala/ticket/1425
>>
>
> I should have mentioned option #4:
>
> (new RichString(s)).map ...

Option #5, which I prefer, is (s : RichString).map ...

The names of implicits really shouldn't matter, so I prefer not to
invoke them directly if I can avoid it.


Gmane