kschiess / parslet

A small PEG based parser library. See the Hacking page in the Wiki as well.
kschiess.github.com/parslet
MIT License
809 stars 95 forks source link

Parslet::Slice should be kind_of String #43

Closed postmodern closed 13 years ago

postmodern commented 13 years ago

Parslet::Slice does a good job mimicing String, except that it does not inherit from String. This breaks code that explicitly checks for obj.kind_of?(String).

floere commented 13 years ago

Yes, and this is a good thing in my book. The agreement is to see if an object answers #to_str to see if it is string-like.

So instead of doing explicit type checking you do message based "type checking", obj.respond_to?(:to_str) instead of obj.kind_of?(String). I just want my thing to quack like a duck, not necessarily be a duck.

postmodern commented 13 years ago

OK. I will have to be more careful when passing Parslet data to brittle interfaces (like Rack).

floere commented 13 years ago

Note that this is just my personal opinion, @kschiess might think differently about that.

Rack does type checking with kind_of? ?

kschiess commented 13 years ago

parslet implements several messages sent to slices differently than string does. Subtypes should behave like the supertypes, extending their functional domain, not limiting it. Since this is what parslet does, Parslet::Slice is no string.

It's really close though and it can be cast to_s. Have you got a concrete message you would like to send to slices and that should be implemented?