Re: __tostring does not work with complex

  • From: Simon Schick <demwizzy@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 23 Jul 2015 15:41:03 +0200

Oh, I thought complex numbers had their own metatable, forgot all cdata
share one.

On 23 July 2015 at 14:59, Mike Pall <mike-1507@xxxxxxxxxx> wrote:

Simon Schick wrote:
"Note that pre-defined operations on types have precedence..."

Yes, this applies here. Also, complex is a bit of a special case
in many ways. It's treated like a scalar, but behind the scenes
it's really an immutable array plus a struct facade.

As a workaround you can get the complex metatable via debug.getmetatable
and set your custom tostring function there:

Err, no ... this will change the metatable for ALL cdata types.
Changing that is not a good idea.

--Mike


Other related posts: