Skip to content

Instantly share code, notes, and snippets.

@LLFourn
Last active February 19, 2016 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LLFourn/9e08d3a94b08af9d5c38 to your computer and use it in GitHub Desktop.
Save LLFourn/9e08d3a94b08af9d5c38 to your computer and use it in GitHub Desktop.
Why I want to negate Types
# introspecting the return vale of a sub
my $return-descr = do given &foo.returns {
when $_ !=== Any { "the programmer has not specified the return value" }
when Any:D { "returns a definite" }
when $_ !~~ Any:D { "returns a type object" }
when Any { "may return either a type object or a definite"}
}
# I would prefer to use something like ¬ (returns a subset type matching anything that doesn't match it)
when ¬Any:U
when Any:D
when ¬Any:D
when Any
#more examples out of context
method parameter(Parameter:D \p --> Docbrew::Parameter:D) {
my %h := %(
orig => p,
type => p.type,
(why => .contents.join("\n") with p.WHY),
(default => p.default if p.default !=== Any),
do with p.constraints { do constraints => $_ if .elems != 1 or .[0].Str ne 'all()' },
|do for <optional named> -> $attr {
do if p."$attr"() -> $val {
$attr => $val
}
}
);
Docbrew::Parameter.new(|%h);
}
method !normalize-code(Code:D $code,Str :$called-as --> Docbrew::Code:D){
my $tmp = \(params => self.signature($code.signature));
my %node = (
orig => $code,
(given $code.signature.returns { if $_ !=== Any { returns => $_ } } ),
(:$called-as with $called-as)
);
...
}
# does it twice
multi trait_mod:<is>(Mu:U $class,:$schema-node!) is export(:MANDATORY) {
my $schema-WHO = $*GLOBALish.WHO<OO-SCHEMA>.WHO;
if (my $node = $schema-WHO{$class.^shortname}) !=== Any {
$*W.install_lexical_symbol($*UNIT,'OO-SCHEMA-ENTRY',$class.^name);
for $node.^parents(:local(1)) {
my $parent = .^load-node-class;
if $parent !=== Any {
$class.^add_parent($parent);
}
}
$class.^add_parent($node);
} else {
die "couldn't find {$class.^shortname} in {$schema-WHO.gist}";
}
}
@LLFourn
Copy link
Author

LLFourn commented Feb 19, 2016

note I realised that my first example is totally bogus. It's much harder to introspect return values:

sub introspect-return(&sub) {
    say &sub.signature.gist ~ " "  ~
    do given &sub.returns {
        when Nil        { "returns nothing" }
        when $_ =:= Mu  { "the programmer has not specified the return value" }
        when .HOW ~~ Metamodel::DefiniteHOW {
            .^definite ??
            "Returns a defined value matching {.^base_type.^name }" !!
            "Returns a type matching {.^base_type.^name }"
        }
        when Mu   { "returns either a defined or undefed {.^name}"}
    }
}
for (
    sub (){},
    sub (--> Nil )  { },
    sub (--> Str:D) { },
    sub (--> Str:U) { },
    sub (--> Str)   { },
)  { .&introspect-return }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment