Home Online Manual
Top
Back: Commands for user defined types
Forward: cone
FastBack: vector
FastForward: cone
Up: User defined types
Top: Singular Manual
Contents: Table of Contents
Index: Index
About: About this document

4.22.5 Assignments for user defined types

By default, only objects of the same (user defined) type can be assigned, there is no automatic type conversion as for the kernel data types.

But the operator = can be overidden in oder to write custom constructors (the custom contructor does not apply to assignments of the same type): via system("install", user_type ,"=" ,p,1);. The user_type has to be given as a string.


Example:

 
  newstruct("wrapping","poly p");
  proc wrap(poly p)
  {
    wrapping w; w.p = p;
    return  (w);
  }
  system("install", "wrapping", "=", wrap, 1);
  ring r = 0,x,dp;
  wrapping w = x+1;
  w;
==> p=x+1
  w = int(1); // via conversion int->poly
  w;
==> p=1
  w=number(2); // via conversion number->poly
  w;
==> p=2

The user defined procedure for = provides also generic type conversions: hh A=hh(b); is equivalent to hh tmp=b; hh A=tmp; kill tmp;.