Dave Thomas
9/9/2003 4:00:00 AM
On Monday, September 8, 2003, at 10:35 PM, Yukihiro Matsumoto wrote:
>
> I agree with usefulness of user-defined literals, and this is much
> disciplined than generic reader macros, but still we need to discuss
> pros and cons.
>
A suggestion for a convention: rather than go down the strange
punctuation route (%Y{...} etc), could we just not adopt the convention
that, where sensible, each class that can construct instances from a
string should define a top-level method with the same name as the class.
So, if I wrote a class called Caml, I'd also define a top-level method,
also called Caml, something like:
class Caml
def Caml.from_string(str)
# .. check string format ..
self.new(str) # or something similar
end
end
def Caml(str)
Caml.from_string(str)
end
Then I could get the effect of literals using:
c = Caml "one hump or two"
If it was appropriate, the top-level method could be memoized, so we
could chose to have new objects created on each call or one object per
literal.
Cheers
Dave