logoalt Hacker News

librastevetoday at 3:04 PM1 replyview on HN

sure...

  role Fish { has Str $.name }

  class Starfish  does Fish { has Str $.favourite-colour; }
  class Jellyfish does Fish { has Bool $.jiggly }

  sub handle-fish(Fish $fish) {
    given $fish {
      when Starfish  { say .favourite-colour }
      when Jellyfish { say .name }
    }   
  }

  handle-fish Starfish.new: :name("Lucy"), :favourite-colour("Pink");
I would probably reach for multi-dispatch...

  role Fish { has Str $.name }

  class Starfish  does Fish { has Str $.favourite-colour; }
  class Jellyfish does Fish { has Bool $.jiggly }

  multi sub handle-fish(Starfish  $fish) { say $fish.favourite-colour }
  multi sub handle-fish(Jellyfish $fish) { say $fish.name }

  handle-fish Starfish.new: :name("Lucy"), :favourite-colour("Pink");

Replies

librastevetoday at 3:14 PM

Here's the other Gleam concurrency example in Raku for good measure:

  my @promises;

  sub MAIN() {
    # Run loads of green threads, no problem
    for ^200_000 {
      spawn-greeter($++);
    }

    await Promise.allof(@promises);
  }

  sub spawn-greeter($i) {
    @promises.push: start {
      say "Hello from $i";
    }
  }