Newbie questions about Pliant
assignment operator
I have a datatype SV, and I need to execute different code when I assigned integer into it, or when I assign a string into it, etc. |
Message posted by reitman on 2003/08/16 16:24:39 |
var SV sv1 sv2 sv1 := 10 sv2 := "10"
should execute the code
lperl_sv_setiv sv1 10 lperl_sv_setpv sv2 "10"
Whats the easiest way to accomplish this ?
Related question. I am continuing work on embedding perl in pliant. Now I am at the array implementation, which is a proxy to an array in the perl space. I would like to have the following code to work,
gvar PerlArray a a:2 := 10
what should happen is, a:2 should return an SV, and I should assign 10 into this SV. The code generated is
var Address ptr_to_sv var SV sv ptr_to_sv := lperl_av_fetch a:av 2 sv := ptr_to_sv map SV lperl_sv_setiv sv 10
I have looked at array.pli, and wrote this,
method a '' i -> sv arg PerlArray a arg_C SV sv var Address ptr var SV sv_tmp ptr := lperl_av_fetch a:av i sv_tmp := ptr map Address # auto-casting from Address to SV (addressof Pointer:SV sv) map Address := sv_tmp
I get confused over what I am returning in 'sv'. Should I just be setting sv := sv_tmp for the code to generate what I am expecting ? Or should I be returning ptr, and the special arg_C will make it act as a C++ reference ?
Thanks, Boris
|
Message posted by hubert.tonneau on 2003/08/16 18:46:29 |
The biggest problem for me is to understand what your 'SV' data type is intended to code.
> Whats the easiest way to accomplish this ?
Define casting functions from 'Str' and 'Int' to 'SV'
function 'cast SV' i -> sv arg Int i ; arg SV sv reduction lperl_sv_setiv sv i
> I get confused over what I am returning in 'sv'
Think about:
method a '' i -> sv arg PerlArray a ; arg_C SV sv
as basically the same as:
method a '' i -> sv arg PerlArray a ; arg Pointer:SV sv
|
Message posted by reitman on 2003/08/17 02:36:23 |
About SV: if I was in C, I would do, typedef Address SV
but in Pliant I did, type SV field Address a
since I din't know how to accomplish the typedef in Pliant.
All operations on an SV happen through the C-wrapped API. like sv := 10 becomes (lperl_setiv sv 10)
|
Message posted by hubert.tonneau on 2003/08/17 08:42:39 |
> if I was in C, I would do, > typedef Address SV
In Pliant, just do:
alias SV Address
and it will say that 'SV' is a new name of 'Address'.
Also, my point of view is that using a new name is generaly a bad idea, because people tend to believe that it's enough to be later abble to change the new type to something else, but most of the time, it's not. |