Created
September 30, 2018 01:05
-
-
Save timo/b3853b19c19419ba9630f583f02824bf to your computer and use it in GitHub Desktop.
#perl6 use a hash as if it were an object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit class HashyObject; | |
has %!storage; | |
submethod TWEAK(*%data) { %!storage = %data } | |
submethod FALLBACK($name, |c) is rw { %!storage{$name} }; | |
method perl { | |
self.^name ~ ".new(" ~ %!storage.pairs.map(*.perl).join(", ") ~ ")" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use HashyObject; | |
my HashyObject $a .= new(cookie => "keks"); | |
$a.hello = "grüße"; | |
$a.goodbye = "auf wiedersehen"; | |
dd $a; | |
# OUTPUT: | |
# HashyObject $a = HashyObject.new(:hello("grüße"), :goodbye("auf wiedersehen"), :cookie("keks")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment