Greetings,
In trying to hack closure trait support into pugs, I have some questions about closure traits, variable with "will" traits and introspection. (Apologies if some of this has been discussed on the list before -- I'm just going off of the synopses, which if definite clarification on some of these issues has been made, should probably be updated to reflect the decisions made.)
Firstly, it is suggested in S04 that variables indicated with a "will" predicate contribute to the corresponding block-level trait. I.e., if we have the following bit of code:
if $dbh {
my $sth will undo {$dbh.rollback} will keep {$dbh.commit} = FIRST {$dbh.prepare($query)};
UNDO {
say "DB error!";
}
KEEP {
say "We're good!";
}
}
Then the block has in effect 5 total traits, 2 UNDO block, 2 KEEP blocks and 1 FIRST blocks. From what I understand, the blocks for each trait are executed in FIFO order, thus we would rollback before we report the error in this contrived example.
Questions:
1) What type of introspection, if any, are we providing to the language level? I.e., are we providing something along the lines of
%traits = &?BLOCK.traits
where %traits is keyed on trait name (FIRST, LAST, whatever) and in turn is an array of closures? This would mean, for instance that we could say
&?BLOCK.traits<FIRST>
to get the current block's FIRST closures, if any. When parsing the block for traits, coming across a new FIRST block would be akin to saying:
push &?BLOCK.traits<FIRST>, {...block contents...}
Specifically, I'm looking for definition of the syntax, which is only alluded to in the Synopsis.
2) If we accept the introspection at the block-level above, it seems clear that we should also accept the same .traits method on variables. I.e., in the above DBI example, we should get back the closure(s) for undoing by referring to $sth.traits<UNDO>. Is a variable-level trait a single entry, or can we have multiple "will undo {...}" predicates on a single variable? (The utility of such is left as an exercise to the reader.)
3) User-definable traits. Now, this may be a closed domain of sorts, but do we need to allow for the possibility of user-defined traits? (I'm thinking here of variable-level "will" predicates.) If so, do user-defined traits get normalized to UPPER? It would seem like we would want consistency here, because if "will undo {...}" and "UNDO {...}" get stored in the same trait slot, we're obviously transforming one of the identifiers -- should this behavior be specific to our "built-in" ones, or to all traits?
4) Which of the closure traits are supported as "will" predicates on variables? Not all of the closure traits make sense on the variable-level -- this information will be useful when trying to parse the "will" predicates.
Thanks,
David Christensen