Re: structure pointers and metatypes using ffi

  • From: Mike Pall <mike-1205@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Mon, 14 May 2012 11:22:03 +0200

William Adams wrote:
> Is there a way to coerce pointer comparisons to use the '__eq'
> metamethod if it is defined on the underlying type?

No, because p1 == p2 already has a fixed meaning, i.e. pointer
comparison.

The general rule is that you cannot override basic behavior with
metatypes. Otherwise I'd have to provide a rawequal() function or
something like that for every operator. Or you wouldn't be able to
use an actual pointer comparison in the metamethod itself. Also,
what should happen if you compare two compatible pointers, but with
different metatypes? I think all of that would get rather confusing.

The problem here is that you get some sugar for handling structs
like struct pointers. But then you don't get the reverse sugar in
some other contexts. I realize this is a tough implementation
choice. OTOH I don't want this to become C++, where you never know
what really happens in your code, even if you write something
innocent as 'a.b'. Extra power comes with extra liability.

--Mike

Other related posts: