HaloO Damian,
you wrote:
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) } }
What is the return type of &*coerce:as?
1: the implicit ::?CLASS, but how is that constructed from Language subclasses?
2: infered from &Language::serialize<Any> which delivers Str or Perl but Str or some such?
3: Str, which doesn't need meta information and is the (final) intension anyway
I assume 1, but is that right? 2 and 3 contradict the general idea of &coerce:as which returns the type of the rhs argument.
What is entered into the MMD tables for &*coerce:as a: 2-ary &*coerce:as<Any,Language> b: parametric 2-ary &*coerce:as<Any,Source[Language]> c: parametric 2-ary &*coerce:as<Source[Language],Any> d: 3-ary &*coerce:as<Source,Any,Language>
and would cases b and c be written as &*coerce:as<Source><Any,Language>. I assume case b is right, or actually instanciations of that for concrete values of ::To.
Then you could just write:
my $good = $x as Source[Perl]; my $bad = $x as Source[C::Sharp]; my $ugly = $x as Source[Lisp];
The type of e.g. $good is then Source[Perl], right?
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) { ... } }
Again, the return type of &YAML::serialize<$> is taken from the lexical ::?CLASS which is of course YAML?
Regards, -- TSa (Thomas Sandlaß)