Top
Back: shared expressions
Forward: reference and shared related functions
FastBack: pyobject
FastForward:
Up: countedref
Top: Singular Manual
Contents: Table of Contents
Index: Index
About: About this document

4.28.5 reference and shared operations

All operations of the underlying objects are forwarded by reference and shared objects. THis kind of dereferencing is done automatically in most cases:


Example:
 
system("reference"); system("shared");
int i = 2;
reference two = i;
shared three = 3;

two * three;
==> 6
two ^ three;
==> 8
two ** three;
==> 8

two + two;
==> 4
two - two;
==> 0

ring r = 0, (x,y,z), dp;
poly p = x + y + z;
reference ref = p;
shared zvar =z;
subst(ref, x,1, y,2, zvar,3);
==> 6

In some cases references have to be dereferenced explicitly. For instance, this is the case for n-ary function calls not starting with a reference or shared object. You can use the link operator or a type cast to work around this. In contrast, some constructs like left-hand subexpressions prematurely evaluate. You can avoid this by using the def operator or by explicitly type casting to reference.

 
system("reference"); system("shared");
ring r = 0, (x,y,z), dp;
poly p = x + y + z;
shared xsh = x;
subst(p, xsh,1, y,2, z,3);        // fails
==>    ? subst(`poly`,`shared`,`int`) failed
==>    ? expected subst(`poly`,`poly`,`poly`)
==>    ? expected subst(`matrix`,`poly`,`int`)
==>    ? error occurred in or before ./examples/reference_and_shared_operatio\
   ns_1.sing line 5: `subst(p, xsh,1, y,2, z,3);        // fails`
subst(p, poly(xsh),1, y,2, z,3);  // good
==> 6
subst(p, link(xsh),1, y,2, z,3);  // fine
==> 6

list ll = list(xsh, xsh, xsh);
ll[1] = y;      // replaced only first entry
ll;
==> [1]:
==>    y
==> [2]:
==>    x
==> 
==> [3]:
==>    x
==> 
shared(ll[2]) = z;    // replaces the others
ll;
==> [1]:
==>    y
==> [2]:
==>    z
==> 
==> [3]:
==>    z
==> 
def(ll[2]) = x;       // generic alternative
ll;
==> [1]:
==>    y
==> [2]:
==>    x
==> 
==> [3]:
==>    x
==> 

In particular, explicit dereferencing is useful to distinguish between typecasting and nested constructings.

 
system("reference"); system("shared");
shared shl = list(1);
shl;
==> [1]:
==>    1
==> 
list(shl);  // wraps 'shl' by a list
==> [1]:
==>    [1]:
==>    1
==> 
link(shl);  // extract the list in 'shl'
==> [1]:
==>    1


Top Back: shared expressions Forward: reference and shared related functions FastBack: pyobject FastForward: Up: countedref Top: Singular Manual Contents: Table of Contents Index: Index About: About this document
            User manual for Singular version 4.3.1, 2022, generated by texi2html.