Skip to content

Instantly share code, notes, and snippets.

@Xliff
Last active April 27, 2021 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xliff/30478a31e96e71132d9e814a722ae3c3 to your computer and use it in GitHub Desktop.
Save Xliff/30478a31e96e71132d9e814a722ae3c3 to your computer and use it in GitHub Desktop.

I need access to self, but can't get to it because I am stuck in the non-instanced state to take advantage of compile-time access. Is there a way to pull up self, here?

Given:

sub buildAccessors(\O, @attributes) {
	my $proxy = \attr -> {
		method :: {
			Proxy.new:
				FETCH => $,    { attr.get_value(O) },
				STORE => $, \v { attr.set_value(O, v) };
		}
	}

	for @attributes -> $k, \v {
		next unless .name.substr(0, 2) eq <$! %! &! @!>.any;

		O.^add_method(
			.name.substr(2),
			trait_mod:<is>($proxy($_), :rw) #= $proxy() returns Method
		)
	}
}

class EContactAddress is repr<CStruct> is export {
	#! cw: Needs accessors!
	has Str $!address_format; #= the two letter country code that determines the format/meaning of the following fields
	has Str $!po;
	has Str $!ext;
	has Str $!street;
	has Str $!locality;
	has Str $!region;
	has Str $!code;
	has Str $!country;

	buildAccessors(self, ::?CLASS.^attributes);
}

Any ideas, #raku?

Thanks

@b2gills
Copy link

b2gills commented Apr 27, 2021

I'm not entirely sure what you're trying to do, but are you sure that you can't pass in ::?CLASS instead of self?

After all .^add_method is for adding methods to a class, not an instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment