caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] How safe is caml_get_public_method() to check if an object has a method?
@ 2016-03-07  2:43 Goswin von Brederlow
  2016-03-07  5:05 ` Jacques Garrigue
  0 siblings, 1 reply; 3+ messages in thread
From: Goswin von Brederlow @ 2016-03-07  2:43 UTC (permalink / raw)
  To: Ocaml Mailing List

Hi,

/usr/include/caml/mlvalues.h says:

CAMLextern value caml_get_public_method (value obj, value tag);
/* Called as:
   caml_callback(caml_get_public_method(obj, caml_hash_variant(name)), obj) */
/* caml_get_public_method returns 0 if tag not in the table.
   Note however that tags being hashed, same tag does not necessarily mean
   same method name. */

In QT5 the classes have virtual methods that can be overloaded. I'm
considering allowing the ocaml classes to have methods by the same
name. The C glue would then check if the ocaml class has such a method
and call it. Otherwise the original method from QT5 would be called.

The question now is wether that is save to do? Is it save as long as
the names produce unique hash_variant values? Or are the hash tables
in objects only parts of the bits of the hash_variant and therefore
collide far more often (given a non-existant method name)?


MfG
	Goswin

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] How safe is caml_get_public_method() to check if an object has a method?
  2016-03-07  2:43 [Caml-list] How safe is caml_get_public_method() to check if an object has a method? Goswin von Brederlow
@ 2016-03-07  5:05 ` Jacques Garrigue
  2016-03-07  8:52   ` Goswin von Brederlow
  0 siblings, 1 reply; 3+ messages in thread
From: Jacques Garrigue @ 2016-03-07  5:05 UTC (permalink / raw)
  To: Goswin von Brederlow; +Cc: OCaml Mailing List

On 2016/03/07 11:43, Goswin von Brederlow wrote:
> 
> Hi,
> 
> /usr/include/caml/mlvalues.h says:
> 
> CAMLextern value caml_get_public_method (value obj, value tag);
> /* Called as:
>   caml_callback(caml_get_public_method(obj, caml_hash_variant(name)), obj) */
> /* caml_get_public_method returns 0 if tag not in the table.
>   Note however that tags being hashed, same tag does not necessarily mean
>   same method name. */
> 
> In QT5 the classes have virtual methods that can be overloaded. I'm
> considering allowing the ocaml classes to have methods by the same
> name. The C glue would then check if the ocaml class has such a method
> and call it. Otherwise the original method from QT5 would be called.
> 
> The question now is wether that is save to do? Is it save as long as
> the names produce unique hash_variant values? Or are the hash tables
> in objects only parts of the bits of the hash_variant and therefore
> collide far more often (given a non-existant method name)?


The function used is exactly hash_variant, so if it gives different results on all
your method names, this is safe.
For debugging purposes, I'm also considering adding the method names
as string to the vritual method table. It could help, but you can already extract
all the ocaml method names from your program, and check that there is
no discrepancy. (The probability of having a conflict is very low.)

Jacques


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] How safe is caml_get_public_method() to check if an object has a method?
  2016-03-07  5:05 ` Jacques Garrigue
@ 2016-03-07  8:52   ` Goswin von Brederlow
  0 siblings, 0 replies; 3+ messages in thread
From: Goswin von Brederlow @ 2016-03-07  8:52 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: OCaml Mailing List

On Mon, Mar 07, 2016 at 02:05:48PM +0900, Jacques Garrigue wrote:
> On 2016/03/07 11:43, Goswin von Brederlow wrote:
> > 
> > Hi,
> > 
> > /usr/include/caml/mlvalues.h says:
> > 
> > CAMLextern value caml_get_public_method (value obj, value tag);
> > /* Called as:
> >   caml_callback(caml_get_public_method(obj, caml_hash_variant(name)), obj) */
> > /* caml_get_public_method returns 0 if tag not in the table.
> >   Note however that tags being hashed, same tag does not necessarily mean
> >   same method name. */
> > 
> > In QT5 the classes have virtual methods that can be overloaded. I'm
> > considering allowing the ocaml classes to have methods by the same
> > name. The C glue would then check if the ocaml class has such a method
> > and call it. Otherwise the original method from QT5 would be called.
> > 
> > The question now is wether that is save to do? Is it save as long as
> > the names produce unique hash_variant values? Or are the hash tables
> > in objects only parts of the bits of the hash_variant and therefore
> > collide far more often (given a non-existant method name)?
> 
> 
> The function used is exactly hash_variant, so if it gives different results on all
> your method names, this is safe.
> For debugging purposes, I'm also considering adding the method names
> as string to the vritual method table. It could help, but you can already extract
> all the ocaml method names from your program, and check that there is
> no discrepancy. (The probability of having a conflict is very low.)
> 
> Jacques

That is good to know. Thx.

MfG
	Goswin

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-03-07  8:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-07  2:43 [Caml-list] How safe is caml_get_public_method() to check if an object has a method? Goswin von Brederlow
2016-03-07  5:05 ` Jacques Garrigue
2016-03-07  8:52   ` Goswin von Brederlow

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).