Re: Class attribute introspection
This and the response from Elizabeth raise in my mind the following question: To what extent are papers/specifications from other parts of the computer science world implicitly a part of the Perl6 specification? I found references to 'getters and setters' in the synopsis on objects, but without the extra references I was unable to do what I wanted in Perl6. As PM said, whatever answer was given to my MOP question, it should be included in one of the synopses, thus making MOP functionality explicitly a part of perl6 synopses. Richard On 10/28/2013 09:42 PM, Carl Mäsak wrote: The MOP is an API to the object-oriented system. With it, you can query classes and their attributes and methods about their properties. It can also be used to create whole new classes (or other types) programatically. Languages that have a MOP have a great advantage because, even though the object system is in a sense always arbitrary, at least now it's programmable. // Carl On Mon, Oct 28, 2013 at 2:17 PM, Richard Hainsworth rich...@rusrating.ru wrote: Moritz, You are the everflowing font of knowledge. Thanks. However, I read the synopsis on objects and did not find the .get_value method. Pardon the ignorance, but what is the MOP. I sometimes get floored by the jargon. I read about the indirection for methods, but how does that relate to attributes? Richard On 10/28/2013 01:45 PM, Moritz Lenz wrote: Hi Richard, On 10/28/2013 08:07 AM, Richard Hainsworth wrote: Perhaps I am using class incorrectly, but I set up a class, then change some of the parameters in an instance of the class. Next I would like to discover what the current state of the instance is. There is a way to introspect through the MOP: class A { has $!x = 42; }; my $obj = A.new; say A.^attributes[0].get_value($obj); It's not straight forwards, and that's actually a feature :-) The usual way to go is through the accessors, and indirect method calls with $obj.$name(); Cheers, Moritz
Class attribute introspection
Perhaps I am using class incorrectly, but I set up a class, then change some of the parameters in an instance of the class. Next I would like to discover what the current state of the instance is. However, I could find no specification of how to access multiple attributes, as opposed to multiple uses of methods. Assume, class A {has $.a; has $.b; method abc {say 'some'} }; my A $x .=new(:a(5), :b(6)); Note that indirection is possible with class methods, using $x.$methodname, but the same does not exist for attributes. It is possible to get a list of attributes with $x.^attributes but how then to turn that list into accessors? I found the following works inside the class definition: class B {has $.a; has $.b; method state {for self.^attributes { say \$x$_ = ~ eval qq[ $_ ] } } }; my B $x .=new(:a(5), :b(66)); $x.state; but that seems to be a bit of a hack. Also I have not found a nice way to achieve the same outside the class definition What have I not seen? Regards, Richard finanalyst
Re: Class attribute introspection
Hi Richard, On 10/28/2013 08:07 AM, Richard Hainsworth wrote: Perhaps I am using class incorrectly, but I set up a class, then change some of the parameters in an instance of the class. Next I would like to discover what the current state of the instance is. There is a way to introspect through the MOP: class A { has $!x = 42; }; my $obj = A.new; say A.^attributes[0].get_value($obj); It's not straight forwards, and that's actually a feature :-) The usual way to go is through the accessors, and indirect method calls with $obj.$name(); Cheers, Moritz
Re: Class attribute introspection
Moritz, You are the everflowing font of knowledge. Thanks. However, I read the synopsis on objects and did not find the .get_value method. Pardon the ignorance, but what is the MOP. I sometimes get floored by the jargon. I read about the indirection for methods, but how does that relate to attributes? Richard On 10/28/2013 01:45 PM, Moritz Lenz wrote: Hi Richard, On 10/28/2013 08:07 AM, Richard Hainsworth wrote: Perhaps I am using class incorrectly, but I set up a class, then change some of the parameters in an instance of the class. Next I would like to discover what the current state of the instance is. There is a way to introspect through the MOP: class A { has $!x = 42; }; my $obj = A.new; say A.^attributes[0].get_value($obj); It's not straight forwards, and that's actually a feature :-) The usual way to go is through the accessors, and indirect method calls with $obj.$name(); Cheers, Moritz
Re: Class attribute introspection
On Mon, Oct 28, 2013 at 05:17:37PM +0400, Richard Hainsworth wrote: Pardon the ignorance, but what is the MOP. I sometimes get floored by the jargon. Whatever answer we get should go into S99. https://github.com/perl6/specs/blob/master/S99-glossary.pod Pm
Re: Class attribute introspection
Maybe http://en.wikipedia.org/wiki/Metaobject is a good start for reading up on what a MOP (Meta-Object Protocol) is. Liz === On 28 Oct 2013, at 14:17, Richard Hainsworth rich...@rusrating.ru wrote: Moritz, You are the everflowing font of knowledge. Thanks. However, I read the synopsis on objects and did not find the .get_value method. Pardon the ignorance, but what is the MOP. I sometimes get floored by the jargon. I read about the indirection for methods, but how does that relate to attributes? Richard On 10/28/2013 01:45 PM, Moritz Lenz wrote: Hi Richard, On 10/28/2013 08:07 AM, Richard Hainsworth wrote: Perhaps I am using class incorrectly, but I set up a class, then change some of the parameters in an instance of the class. Next I would like to discover what the current state of the instance is. There is a way to introspect through the MOP: class A { has $!x = 42; }; my $obj = A.new; say A.^attributes[0].get_value($obj); It's not straight forwards, and that's actually a feature :-) The usual way to go is through the accessors, and indirect method calls with $obj.$name(); Cheers, Moritz
Re: Class attribute introspection
The MOP is an API to the object-oriented system. With it, you can query classes and their attributes and methods about their properties. It can also be used to create whole new classes (or other types) programatically. Languages that have a MOP have a great advantage because, even though the object system is in a sense always arbitrary, at least now it's programmable. // Carl On Mon, Oct 28, 2013 at 2:17 PM, Richard Hainsworth rich...@rusrating.ru wrote: Moritz, You are the everflowing font of knowledge. Thanks. However, I read the synopsis on objects and did not find the .get_value method. Pardon the ignorance, but what is the MOP. I sometimes get floored by the jargon. I read about the indirection for methods, but how does that relate to attributes? Richard On 10/28/2013 01:45 PM, Moritz Lenz wrote: Hi Richard, On 10/28/2013 08:07 AM, Richard Hainsworth wrote: Perhaps I am using class incorrectly, but I set up a class, then change some of the parameters in an instance of the class. Next I would like to discover what the current state of the instance is. There is a way to introspect through the MOP: class A { has $!x = 42; }; my $obj = A.new; say A.^attributes[0].get_value($obj); It's not straight forwards, and that's actually a feature :-) The usual way to go is through the accessors, and indirect method calls with $obj.$name(); Cheers, Moritz