They do two different things.  Style 1 will throw a dispatch error if $
node.name has a value of 'three', where Style 2 will do nothing in that
case.

On Thu, Nov 15, 2018 at 7:55 PM Richard Hainsworth <rnhainswo...@gmail.com>
wrote:

> Suppose I have a sub called C<handle> that runs different code depending
> on the content of an argument.
>
> There are two styles in Perl 6 to code this and my question is whether
> one is more efficient (speed/memory) than the other.
>
> In this example, one relies on multiple dispatch, while the other an
> explicit control statement.
>
> Putting aside clarity considerations, is there any performance reason to
> prefer one style over another?
>
> Style 1
>
> multi sub handle( $node WHERE *.name ~~ 'one' ) {
>
>      'this is a wonderful sub'
>
> }
>
> multi sub handle( $node WHERE *.name ~~ 'two' ) {
>
>     'twas brillig and the slivy toves did gyre'
>
> }
>
>
> Style 2
>
> sub handle ( $node ) {
>
>      given $node.name {
>
>          when 'one' { 'this is a wonderful sub' }
>
>          when 'two' { 'twas brillig and the slivy toves did gyre' }
>
>      }
>
> }
>

Reply via email to