logoalt Hacker News

tasukitoday at 2:24 PM1 replyview on HN

> But I get the feeling that Raku is underappreciated / dismissed by many due to the perl5 / perl6 history.

Yes that would be me! If you like making these comparisons, can you write the following pattern matching in Raku?

    import gleam/io
    
    pub type Fish {
      Starfish(name: String, favourite_colour: String)
      Jellyfish(name: String, jiggly: Bool)
    }
    
    pub fn main() {
      handle_fish(Starfish("Lucy", "Pink"))
    }
    
    fn handle_fish(fish: Fish) {
      case fish {
        Starfish(_, favourite_colour) -> io.println(favourite_colour)
        Jellyfish(name, ..) -> io.println(name)
      }
    }

Replies

librastevetoday at 3:04 PM

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");
show 1 reply