poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [RFC] Methods and functions


From: Jose E. Marchesi
Subject: Re: [RFC] Methods and functions
Date: Tue, 05 May 2020 20:46:48 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

    
    > *** A couple of restrictions
    >
    >      Given the different nature of the variables, functions and methods, 
there
    >      are a couple of restrictions:
    >
    >      a) Methods can't access variables and function defined in the struct 
type.
    >
    >         This is not gratuitous: when you think about the 
mapping/construction
    >         process, you will note these variables and functions are locals 
to the
    >         mapper/constructor, which has already been executed at the time 
we are
    >         in the position to invoke a method!
    >
    >         If you try to access a variable or function defined inside a 
struct
    >         method, you will get a nice "invalid reference to struct
    >         {variable,function}" error message from the compiler.
    >
    
    I am not sure if I understand this correctly and I am very skeptic
    about what I understand.
    
    We keep re-mapping structs, right? I tried the following to make sure
    it works:
    
    (poke) deftype Packet3 = struct { byte magic = 0xab;   byte size;
    defvar real_size = (size == 0xff ? size - 0xff : size + 0x0);
    byte[real_size] payload; };
    (poke) defvar packet3 = Packet3 @ 0#B
    (poke) packet3
    Packet3 {magic=171UB,size=2UB,payload=[99UB,100UB]}
    (poke) packet3.size = 10
    (poke) packet3
    Packet3
    
{magic=171UB,size=10UB,payload=[99UB,100UB,101UB,102UB,103UB,104UB,105UB,106UB,97UB,98UB]}
    
    
    Here, the only connection between the size and the payload is
    real_size.  I am glad to see that the payload is still updated when we 
    change the size.
    
    Still, though, I do not get why we cannot access variables and
    functions of a struct within its method. They are already defined when
    we have the methods. Is this really as gratuitous?
    
They are defined, but they have the value of the latest
remap/construction, because the same mapper/constructor is used to build
the new value.  Thats not very useful, and it is actually dangerous.

Suppose we were allowing accessing variables/functions from the struct,
and we had this:

  deftype Foo =
   struct
   {
     int i;
     defun get_i = int: { return i; }
     method foo = int: { return get_i; }
   };

Then we do this:

(poke) defvar f1 = Foo @ 0#B
(poke) f1
Foo { i = 10 }
(poke)  defvar f2 = Foo @ 12#B
(poke) f2
Foo { i = 20 }
(poke) f1.foo
20

Do you see why?  The last time the closure Foo_constructor ran, it did
set the local variable `i' to 20, and that is what got captured in its
lexical environment.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]