in reply to Re^2: Contexts and Perl 6
in thread Contexts and Perl 6

Actually the current spec allows something along these lines:
sub my_context_sensitive { return $someobject but { method Str { # code for computing the string representation } method list { # code that's called in list context } ... } }

So the cost of that is just creating a closure for each "overloaded" context.

Replies are listed 'Best First'.
Re^4: Contexts and Perl 6
by John M. Dlugosz (Monsignor) on May 18, 2009 at 21:19 UTC
    So, you go through all the trouble of cloning the closures and creating the one-off "butted" type. Even if inlined or the compiler otherwise does know the context, it still can't easily figure out that whole methods can be removed.

    If the function knew it was called in numeric context, it could return Array but compute_length();. If optimized over, the property contains no closures, so is easily cut down to just the desired function call.

      Even if inlined or the compiler otherwise does know the context, it still can't easily figure out that whole methods can be removed.
      Why not?
        The blocks might end up calling each other or otherwise using self and then the ramifications are not easily tracked.

        I like it in principle, but give it a more formal construct that includes the promise that no more than 1 of them will be asked for and they are not interdependant. Said construct could also help with syntactic sugar if the final way of writing the contexualizer methods isn't quite so pretty (simple method names easily conflict with regular use; it's a shame to reserve them).

        —John