Thomas Sandlaß wrote:
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?
Sorry, I was too lazy (well, I'd claim I was thinking at a much higher level, but it amounts to the same ;-) and I made a mistake in the signature of the coercion.
Here's a fully typed, and correctly parameterized, version:
class Source[Language ::To] is Str { multi sub *coerce:as (Any $data, Source[To] ::Lang) returns Source[To] { return Lang.serialize($data) } }
class YAML is Language { method serialize($data) returns Source[YAML] { ... return Source[YAML].new($source_code); } }
Or we could cheat in the coercion, and just do it all with strings:
class Source[Language ::To] { multi sub *coerce:as (Any $data, Source[To] ::Lang) returns Str { return Lang.serialize($data) } }
class YAML is Language { method serialize($data) returns Str { ... return $source_code; } }
As for the MMD table, that's an implementation issue, but presumably it'd have to contain separate entries for each instantiation of the generic Source class:
2-ary &*coerce:as<Any,Source[Perl]> 2-ary &*coerce:as<Any,Source[Python]> 2-ary &*coerce:as<Any,Source[Lisp]> 2-ary &*coerce:as<Any,Source[C::Sharp]> # etc.
or (more likely, given the dynamic nature of Perl) a single entry that's a "trampoline" that instantiates particular parameterizations of the generic class as they're needed:
2-ary &*coerce:as<Any,Source[*]>
Damian