Daniel Ruoso wrote: > Hmm... I think that takes the discussion to another level, and the > question is: > > "what does a capture returns when coerced to a context it doesn't > provide a value for?" > > The easy answer would be undef, empty array and empty hash, but that > doesn't DWIM at all. > > The hard answer is DWIM, and that can be: > > 1) in item context, without an invocant > a) if only one positional argument, return it > b) if only one named argument, return it as a pair > c) if several positional args, but no named args, > return an array > d) if several named args, but no positional args, > return a hash > e) if no args at all, return undefined Object > f) return itself otherwise > 2) in list context, without positional arguments > a) if one or more named arguments, > return a list of pairs > b) return an empty list otherwise > 3) in hash context, without named arguments > a) if there are positional arguments, > return a hash taking key,value. > if an odd number of positional arguments, > last key has an undef Object as the > value and issue a warning. > b) return an empty hash otherwise
Elaborate further to account for the possibility of an invocant. For example, if you have a capture object with just an invocant and you're in list context, should it return the invocant as a one-item list, or should it return an empty list? Should hash context of the same capture object give you a single Pair with an undef value and a a warning, or should it give you an empty hash? I don't mind the dwimmery here, even though that means that you can't be certain that (e.g.) 'list $x' will return the positional parameters in capture object $x. If you want to be certain that you're getting the positional parameters and nothing else, you can say '$x.[]'; if you want to be certain that you're getting the named parameters, you can say '$x.{}'. The only thing lost is the ability to ensure that you're getting the invocant; and it wouldn't be too hard to define, say, a postfix:<_> operator for the Capture object that does so: item($x) # Dwimmey use of item context. list($x) # Dwimmey use of list context. hash($x) # Dwimmey use of hash context. $x._ # the Capture object's invocant, as an item. $x.[] # the Capture object's positional parameters, as a list. $x.{} # the Capture object's named parameters, as a hash. -- Jonathan "Dataweaver" Lang