Thread Hash to Class-Attributes
(21 answers)
Opened by Kuerbis at 2016-01-06 09:57
Mir fällt gerade ein, dass Du den Vorschlag von janus auch mit der "clone"-Idee kombinieren kannst. Dann nimmst du statt einem Hash halt einfach noch eine weitere Klasse. Sieht dann so aus:
Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class MyOptions { has $.length = 10; has $.height; has @.names; } class MyClass { has MyOptions $.options = MyOptions.new; method modify(%changes) { $!options.=clone(|%changes); } } my MyClass $n.=new; say $n.perl; $n.modify({length => 42, names => ['Kai', 'Otto']}); say $n.perl; Ausgabe: Code: (dl
)
1 MyClass.new(options => MyOptions.new(length => 10, height => Any, names => [])) Also alles genau wie du willst, oder? Das funktioniert sogar mit Arrays (siehe "names"). Last edited: 2016-01-18 19:52:15 +0100 (CET) Lieblingsmodule:
![]() ![]() View full thread Hash to Class-Attributes |