help-octave
[Top][All Lists]
Advanced

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

cast on objects


From: Ernst Reissner
Subject: cast on objects
Date: Mon, 01 Dec 2014 12:03:01 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0

Hi all,
I integrated numbers implemented in java into octave,
wrapping them into class objects (for some reason).
This works quite well, but now i realized, that

https://www.gnu.org/software/octave/doc/interpreter/Function-Overloading.html#Function-Overloading

suggests to overwrite method double(...), to allow casting my numbers to
double.
I already had a method doubleValue(..) which did right this, but now i
renamed it to double(..).

In fact I would have expected that cast (given below, i obtained 'type
cast')

function retval = cast (val, typ)

  if (nargin == 2)
    if (ischar (typ))
      if (any (strcmp (typ, {"int8"; "uint8"; "int16"; "uint16";
                             "int32"; "uint32"; "int64"; "uint64";
                             "double"; "single"; "logical"; "char"})))
        retval = feval (typ, val);
      else
        error ("cast: type name '%s' is not a built-in type", typ);
      endif
    else
      error ("cast: expecting TYPE name as second argument");
    endif
  else
    print_usage ();
  endif

endfunction

would work well. I tried

cast(pn(3),"double")

which creates an object via constructor pn(3) wrapping the number 3
and then applies cast which applies function 'double'

So i would have expected that, as documented on the above web site,
cast applies my function double(...) on pn(3), i.e. invokes double(pn(3)).

But: not at all:

octave:17> cast(pn(3),"double")
error: invalid conversion from class to scalar
error: called from:
error:   /usr/share/octave/3.6.4/m/miscellaneous/cast.m at line 34,
column 16


and even

octave:17> double(pn(3))
error: no subsindex method defined for class pn

which i also do not understand.
Ok, i did not overwrite subsindex but why is this needed here????

By the way,
octave:17> doubleValue(pn(3))
ans =  3
works well,
although doubleValue and double are defined by essentially identical
m-files.

So maybe, i do not have one problem but two???

Thanks for your ideas,

Ernst





reply via email to

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