caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] function that works on different record types, assuming they have a given field
@ 2011-03-28 18:16 Joel Reymont
  2011-03-28 18:34 ` David Rajchenbach-Teller
  0 siblings, 1 reply; 5+ messages in thread
From: Joel Reymont @ 2011-03-28 18:16 UTC (permalink / raw)
  To: caml-list

Is it possible to write a function that works on different record types that have, say, a field x of type int?

The records would be in different modules but the field name and type will always be the same. 

	Thanks, Joel

--------------------------------------------------------------------------
- for hire: mac osx device driver ninja, kernel extensions and usb drivers
---------------------+------------+---------------------------------------
http://wagerlabs.com | @wagerlabs | http://www.linkedin.com/in/joelreymont
---------------------+------------+---------------------------------------





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

* Re: [Caml-list] function that works on different record types, assuming they have a given field
  2011-03-28 18:16 [Caml-list] function that works on different record types, assuming they have a given field Joel Reymont
@ 2011-03-28 18:34 ` David Rajchenbach-Teller
  2011-03-28 18:35   ` Joel Reymont
  0 siblings, 1 reply; 5+ messages in thread
From: David Rajchenbach-Teller @ 2011-03-28 18:34 UTC (permalink / raw)
  To: Joel Reymont; +Cc: caml-list

Nope, in OCaml, a field name unambiguously identifies the type of the record. On the other hand, you can obtain the same behavior by using objects instead of records:


 # let f x = x#foo;;
val f : < foo : 'a; .. > -> 'a = <fun>

Hope this helps,
 David

On Mar 28, 2011, at 8:16 PM, Joel Reymont wrote:

> Is it possible to write a function that works on different record types that have, say, a field x of type int?
> 
> The records would be in different modules but the field name and type will always be the same. 
> 
> 	Thanks, Joel
> 
> --------------------------------------------------------------------------
> - for hire: mac osx device driver ninja, kernel extensions and usb drivers
> ---------------------+------------+---------------------------------------
> http://wagerlabs.com | @wagerlabs | http://www.linkedin.com/in/joelreymont
> ---------------------+------------+---------------------------------------
> 
> 
> 
> 
> 
> -- 
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
> 



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

* Re: [Caml-list] function that works on different record types, assuming they have a given field
  2011-03-28 18:34 ` David Rajchenbach-Teller
@ 2011-03-28 18:35   ` Joel Reymont
  2011-03-30 12:38     ` [Caml-list] " Dawid Toton
  0 siblings, 1 reply; 5+ messages in thread
From: Joel Reymont @ 2011-03-28 18:35 UTC (permalink / raw)
  To: David Rajchenbach-Teller; +Cc: caml-list


On Mar 28, 2011, at 7:34 PM, David Rajchenbach-Teller wrote:

> you can obtain the same behavior by using objects instead of records:

Yes, that used to work very well... until I was forced to switch to records :-).

	Thanks, Joel

--------------------------------------------------------------------------
- for hire: mac osx device driver ninja, kernel extensions and usb drivers
---------------------+------------+---------------------------------------
http://wagerlabs.com | @wagerlabs | http://www.linkedin.com/in/joelreymont
---------------------+------------+---------------------------------------





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

* [Caml-list] Re: function that works on different record types, assuming they have a given field
  2011-03-28 18:35   ` Joel Reymont
@ 2011-03-30 12:38     ` Dawid Toton
  2011-03-30 14:15       ` Gabriel Scherer
  0 siblings, 1 reply; 5+ messages in thread
From: Dawid Toton @ 2011-03-30 12:38 UTC (permalink / raw)
  To: caml-list

On 03/28/2011 08:35 PM, Joel Reymont wrote:
> On Mar 28, 2011, at 7:34 PM, David Rajchenbach-Teller wrote:
>
>> you can obtain the same behavior by using objects instead of records:
> Yes, that used to work very well... until I was forced to switch to records :-).
>
When I had this problem I used one common record type:

type ('foo, 'bar') common =
  {foo : 'foo
  ;bar : 'bar
  ;x : int
  }

type with_bar = (unit, string) common
type with_foo = (double, unit) common

However, I can see that with more different independent types this is
going to be quite ugly.

Dawid

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

* Re: [Caml-list] Re: function that works on different record types, assuming they have a given field
  2011-03-30 12:38     ` [Caml-list] " Dawid Toton
@ 2011-03-30 14:15       ` Gabriel Scherer
  0 siblings, 0 replies; 5+ messages in thread
From: Gabriel Scherer @ 2011-03-30 14:15 UTC (permalink / raw)
  To: Dawid Toton; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 1116 bytes --]

Why not pass the value of the "x" field directly to the function, instead of
trying to receive a would-be { x : 'a; .... } parameter ?
If you would have liked the field to be mutable, make it a reference.

On Wed, Mar 30, 2011 at 2:38 PM, Dawid Toton <d0@wp.pl> wrote:

> On 03/28/2011 08:35 PM, Joel Reymont wrote:
> > On Mar 28, 2011, at 7:34 PM, David Rajchenbach-Teller wrote:
> >
> >> you can obtain the same behavior by using objects instead of records:
> > Yes, that used to work very well... until I was forced to switch to
> records :-).
> >
> When I had this problem I used one common record type:
>
> type ('foo, 'bar') common =
>  {foo : 'foo
>  ;bar : 'bar
>  ;x : int
>  }
>
> type with_bar = (unit, string) common
> type with_foo = (double, unit) common
>
> However, I can see that with more different independent types this is
> going to be quite ugly.
>
> Dawid
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

[-- Attachment #2: Type: text/html, Size: 1795 bytes --]

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

end of thread, other threads:[~2011-03-30 14:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-03-28 18:16 [Caml-list] function that works on different record types, assuming they have a given field Joel Reymont
2011-03-28 18:34 ` David Rajchenbach-Teller
2011-03-28 18:35   ` Joel Reymont
2011-03-30 12:38     ` [Caml-list] " Dawid Toton
2011-03-30 14:15       ` Gabriel Scherer

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).