Pattern literals in core

3 views
Skip to first unread message

Christophe Grand

unread,
Apr 30, 2010, 5:26:22 AM4/30/10
to cloju...@googlegroups.com
Hi,

While writing a patch for #326 I was surprised by errors relating to pattern literals becoming another kind of literals at runtime.
I got funny messages such as:
* #"\." is a Character and cannot be cast to Pattern
* #"[.]" is a PersistentVector and cannot be cast to Pattern

The problem is that in most of core.clj pr-on isn't initialized yet and that RT/print don't know how to readably print pattern literals by itself.

May I fill a bug and attach the following patch?

Thanks,

Christophe

--
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.cgrand.net/ (en)

--
You received this message because you are subscribed to the Google Groups "Clojure Dev" group.
To post to this group, send email to cloju...@googlegroups.com.
To unsubscribe from this group, send email to clojure-dev...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/clojure-dev?hl=en.
rt-print-pattern.patch

Rich Hickey

unread,
Apr 30, 2010, 6:35:38 AM4/30/10
to cloju...@googlegroups.com

On Apr 30, 2010, at 5:26 AM, Christophe Grand wrote:

> Hi,
>
> While writing a patch for #326 I was surprised by errors relating to
> pattern literals becoming another kind of literals at runtime.
> I got funny messages such as:
> * #"\." is a Character and cannot be cast to Pattern
> * #"[.]" is a PersistentVector and cannot be cast to Pattern
>
> The problem is that in most of core.clj pr-on isn't initialized yet
> and that RT/print don't know how to readably print pattern literals
> by itself.
>
> May I fill a bug and attach the following patch?
>

Sure, thanks!

Rich

Chouser

unread,
Apr 30, 2010, 8:25:50 AM4/30/10
to cloju...@googlegroups.com
On Fri, Apr 30, 2010 at 5:26 AM, Christophe Grand <chris...@cgrand.net> wrote:
> Hi,
>
> While writing a patch for #326 I was surprised by errors relating to pattern
> literals becoming another kind of literals at runtime.
> I got funny messages such as:
> * #"\." is a Character and cannot be cast to Pattern
> * #"[.]" is a PersistentVector and cannot be cast to Pattern
>
> The problem is that in most of core.clj pr-on isn't initialized yet and that
> RT/print don't know how to readably print pattern literals by itself.
>
> May I fill a bug and attach the following patch?

I assume you're aware that patch will not correctly print all
Patterns. Perhaps that's okay since the complete Pattern printer
remains (in core_print.clj).

--Chouser
http://joyofclojure.com/

Christophe Grand

unread,
Apr 30, 2010, 9:03:53 AM4/30/10
to cloju...@googlegroups.com
On Fri, Apr 30, 2010 at 2:25 PM, Chouser <cho...@gmail.com> wrote:
On Fri, Apr 30, 2010 at 5:26 AM, Christophe Grand <chris...@cgrand.net> wrote:
> Hi,
>
> While writing a patch for #326 I was surprised by errors relating to pattern
> literals becoming another kind of literals at runtime.
> I got funny messages such as:
> * #"\." is a Character and cannot be cast to Pattern
> * #"[.]" is a PersistentVector and cannot be cast to Pattern
>
> The problem is that in most of core.clj pr-on isn't initialized yet and that
> RT/print don't know how to readably print pattern literals by itself.
>
> May I fill a bug and attach the following patch?

I assume you're aware that patch will not correctly print all
Patterns.  Perhaps that's okay since the complete Pattern printer
remains (in core_print.clj).

Yes, it should support all pattern literals though.

As I stated in ticket #327 print-method fails on some patterns too. I think it's really hard to write a correct print-method for all patterns -- I even think it's even impossible once flags come in play:
user=> (.flags (java.util.regex.Pattern/compile "H(?i)e"))
2
user=> (.flags (java.util.regex.Pattern/compile "H(?i)e" java.util.regex.Pattern/CASE_INSENSITIVE))
2
Two different patterns returning the same flags and the same pattern string :-(


Christophe

Chouser

unread,
Apr 30, 2010, 11:04:10 AM4/30/10
to cloju...@googlegroups.com
On Fri, Apr 30, 2010 at 9:03 AM, Christophe Grand <chris...@cgrand.net> wrote:
> On Fri, Apr 30, 2010 at 2:25 PM, Chouser <cho...@gmail.com> wrote:
>>
>> On Fri, Apr 30, 2010 at 5:26 AM, Christophe Grand <chris...@cgrand.net>
>> wrote:
>> > Hi,
>> >
>> > While writing a patch for #326 I was surprised by errors relating to
>> > pattern
>> > literals becoming another kind of literals at runtime.
>> > I got funny messages such as:
>> > * #"\." is a Character and cannot be cast to Pattern
>> > * #"[.]" is a PersistentVector and cannot be cast to Pattern
>> >
>> > The problem is that in most of core.clj pr-on isn't initialized yet and
>> > that
>> > RT/print don't know how to readably print pattern literals by itself.
>> >
>> > May I fill a bug and attach the following patch?
>>
>> I assume you're aware that patch will not correctly print all
>> Patterns.  Perhaps that's okay since the complete Pattern printer
>> remains (in core_print.clj).
>
> Yes, it should support all pattern literals though.

Ok, good point.

> As I stated in ticket #327 print-method fails on some patterns too. I think
> it's really hard to write a correct print-method for all patterns -- I even
> think it's even impossible once flags come in play:
> user=> (.flags (java.util.regex.Pattern/compile "H(?i)e"))
> 2

Wow. :-( Surely that's a bug. But you're right, since the Pattern
object appears to behave differently based on information it
keeps private, there's no way for us to print that correctly 100%
of the time. Very bad. I guess we just have to get as close to
100% as is reasonable.

--Chouser

Christophe Grand

unread,
Apr 30, 2010, 11:56:23 AM4/30/10
to cloju...@googlegroups.com
On Fri, Apr 30, 2010 at 5:04 PM, Chouser <cho...@gmail.com> wrote:
> As I stated in ticket #327 print-method fails on some patterns too. I think
> it's really hard to write a correct print-method for all patterns -- I even
> think it's even impossible once flags come in play:
> user=> (.flags (java.util.regex.Pattern/compile "H(?i)e"))
> 2

Wow.  :-(  Surely that's a bug.  But you're right, since the Pattern
object appears to behave differently based on information it
keeps private, there's no way for us to print that correctly 100%
of the time.  Very bad.  I guess we just have to get as close to
100% as is reasonable.

Maybe we should modify literal support to better handle \Q and \E, I think it remove all complexity from print-method for Patterns.
The change I propose in this patch is to make #"\Q"\E" readable.

With \Q and \E properly handled next step on the asymptotic road to 100% is comments.


Christophe

--
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.cgrand.net/ (en)

0001-make-Q-E-readable.patch
Reply all
Reply to author
Forward
0 new messages