Larry wrote:

> Actually, I'm thinking we should just go with a single method and
> have it merely default to :lang<Perl>.  But .repr is rather ugly.
> How 'bout .pretty instead?  If we made the language the first optional
> argument you could have $x.pretty('Lisp'), $x.pretty('C#'), etc.

Hmmmmm, maybe we shouldn't introduce oxymorons into the language. ;-)

Actually, I'd have thought that the type coercion mechanism might be a
more appropriate way to go here. After all, the serialization of a data
structure is merely a coercion to a subtype of Str. Specifically, I
imagine a parameterized Source subtype:

    class Source[Language ::To] is Str {
        multi sub *coerce:as (Any $data, To ::Lang) {
            return Lang.serialize($data)
        }
    }

Then you could just write:

    my $good = $x as Source[Perl];
    my $bad  = $x as Source[C::Sharp];
    my $ugly = $x as Source[Lisp];

And adding a new serialization would simply mean adding a new Language
subclass with an appropriate .serialize() method:

    class YAML is Language {
        method serialize($data) {
            ...
        }
    }

    class XML is Language {
        method serialize($data) {
            ...
        }
    }

    # etc.

Damian

Reply via email to