This is my attempt to solve Challenge #2 of the weekly.
The challenge is very vague, so I decided to try to implement a scalar value with a memory. It's possible my understanding of how containers should work is flawed, but what I really don't understand, is, why say self.VAR.WHAT
is a Proxy
and not a HistoryProxy
, even when I explicitly say so.
class HistoryProxy is Proxy
{
has @!history;
method HISTORY() { @!history };
method SAVE( $value ) { @!history.push($value) }
}
sub historic(::T $type, $value) {
my T $c-value = $value;
return-rw HistoryProxy.new(
FETCH => method () { $c-value },
STORE => method (T $new-value) {
say self.VAR.WHAT; # Why is this "Proxy" and not "HistoryProxy"?
self.VAR.SAVE( $c-value ); # Why does this not work?
$c-value = $new-value;
}
);
}
my $a := historic(Int, 10);
$a = 12;
$a = 14;
say $a.VAR.HISTORY; #should print [10, 12]
Scalars can be defined as placeholders in Stata that can store a single number or a single piece of string. Matrices, on the other hand, can store multiple numbers or strings. In order to define a scalar in Stata, we use the following syntax:
To create a scalar value function, the “Create” statement is used. These types of functions are included in the code to simplify the code. For example, you may need to carry out a challenging calculation at various steps in your code.
We can use a scalar to store a value defined in a matrix. For example: This scalar, called ‘e’, stores the value present in the first row and second column of matrix ‘d’. ‘e’ would thus store the value of ‘5’ as can be seen from the command:
System-defined functions are built-in functions predefined, and their functionality cannot be changed. On the other hand, user-defined functions are the ones that can be designed to perform a custom task as per our needs. In this article, we will talk about the scalar value function (user-defined functions) and learn how to create it.
This does not help you get the functionality you want, but it does answer your specific questions for now:
say self.VAR.WHAT; # Why is this "Proxy" and not "HistoryProxy"?
I think this is because the Proxy
class is currently not set up to be subclassed. Its new
method basically does a Proxy.new
instead of a self.new
, so it misses the subclassing. Looking into that now.
self.VAR.SAVE( $c-value ); # Why does this not work?
self
is always decontainerized. So you're always seeing the underlying value. If you want to have the actual object, you will need to change the signature of the method
, e.g.:
STORE => method (\SELF: T $new-value) {
and then use:
SELF.VAR
But since the object isn't actually blessed as the subclass, this won't help you much anyway.
UPDATE: https://github.com/rakudo/rakudo/pull/3196 should allow subclassing of Proxy
objects in the future.
UPDATE: with https://github.com/rakudo/rakudo/commit/d00674b31c this Pull Request got merged. It should become available in the 2019.11 Rakudo compiler release.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With