Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A scalar with memory, or how to correctly `tie`

Tags:

raku

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]
like image 309
Holli Avatar asked Sep 23 '19 03:09

Holli


People also ask

What is a scalar in Stata?

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:

How to create a scalar value function in C++?

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.

How to store a value in a matrix using scalar?

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:

What is a scalar value function (user-defined functions)?

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.


Video Answer


1 Answers

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.

like image 175
Elizabeth Mattijsen Avatar answered Dec 22 '22 17:12

Elizabeth Mattijsen