Skip to content

Instantly share code, notes, and snippets.

@vrurg
Created July 4, 2023 00:21
Show Gist options
  • Save vrurg/88ed942d85c897772061f7f92bfb85fd to your computer and use it in GitHub Desktop.
Save vrurg/88ed942d85c897772061f7f92bfb85fd to your computer and use it in GitHub Desktop.
This is an example of creating a generic subset.
# This is an example of how a generic can be used by a nominalizable type from the MOP point of view.
# There is a catch in this example: there is no way to create similar subset via Raku syntax using the legacy grammar.
use nqp;
my \generic = Metamodel::GenericHOW.new_type(:name<T>);
say "Generic's name: ", generic.^name;
sub make-ctx(::T --> Mu) is raw {
# Instantiation of a generic requires a lexical context to work. This is how we can get it dynamically – and we
# do it by using another generic.
nqp::ctxlexpad(nqp::ctx());
}
my \gsubset =
Metamodel::SubsetHOW.new_type:
:name<MySubset>,
:refinee(generic),
:refinement({ .defined });
say "Is the subset {gsubset.^name} generic? ", ? gsubset.^archetypes.generic;
my \StrSS = gsubset.^instantiate_generic(make-ctx(Str));
say "--- StrSS";
say "AA" ~~ StrSS;
say Str ~~ StrSS;
say 42 ~~ StrSS;
my \RealSS = gsubset.^instantiate_generic(make-ctx(Real));
say "--- RealSS";
say 4.2 ~~ RealSS;
say 42 ~~ RealSS;
say 42e-1 ~~ RealSS;
say Real ~~ RealSS;
say "42" ~~ RealSS;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment