* different records, same field name? @ 2008-03-03 16:51 mfmorss 2008-03-03 16:53 ` [Caml-list] " Jon Harrop ` (2 more replies) 0 siblings, 3 replies; 9+ messages in thread From: mfmorss @ 2008-03-03 16:51 UTC (permalink / raw) To: caml-list caml-list I have not used OCaml, just done some reading about it and toyed with the toplevel to see if it might be a useful tool here. I have "Practical OCaml," which unfortunately is a rather wretched reference. In any case, is it possible to define and use different types of records, which nevertheless share some field names? I have had difficulty doing this in the toplevel. ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] different records, same field name? 2008-03-03 16:51 different records, same field name? mfmorss @ 2008-03-03 16:53 ` Jon Harrop 2008-03-03 16:55 ` Basile STARYNKEVITCH 2008-03-03 18:45 ` DooMeeR 2 siblings, 0 replies; 9+ messages in thread From: Jon Harrop @ 2008-03-03 16:53 UTC (permalink / raw) To: caml-list On Monday 03 March 2008 16:51:57 mfmorss@aep.com wrote: > I have not used OCaml, just done some reading about it and toyed with the > toplevel to see if it might be a useful tool here. I have "Practical > OCaml," which unfortunately is a rather wretched reference. > > In any case, is it possible to define and use different types of records, > which nevertheless share some field names? I have had difficulty doing > this in the toplevel. Field names will shadow others so you define the record types in separate modules: module Vec2 = struct type t = {x: float; y: float} end module Vec3 = struct type t = {x: float; y: float; z: float} end { Vec2.x = 2.; y = 3. } -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. http://www.ffconsultancy.com/products/?e ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] different records, same field name? 2008-03-03 16:51 different records, same field name? mfmorss 2008-03-03 16:53 ` [Caml-list] " Jon Harrop @ 2008-03-03 16:55 ` Basile STARYNKEVITCH 2008-03-03 19:32 ` Kuba Ober 2008-03-03 18:45 ` DooMeeR 2 siblings, 1 reply; 9+ messages in thread From: Basile STARYNKEVITCH @ 2008-03-03 16:55 UTC (permalink / raw) To: mfmorss; +Cc: caml-list caml-list mfmorss@aep.com wrote: > > In any case, is it possible to define and use different types of records, > which nevertheless share some field names? I have had difficulty doing > this in the toplevel. It is not possible, for a simple reason: Suppose you have two record type tr1 & tr2 sharing a common field name f type tr1 = { f : int; f1 : string } type tr2 = { f2 : float; f: int } Then the compiler won't be able to infer the type of function ff retrieving this field f let ff x = x.f So the requirement that each field name is different is the price to pay to have type inference. Regards -- Basile STARYNKEVITCH http://starynkevitch.net/Basile/ email: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359 8, rue de la Faiencerie, 92340 Bourg La Reine, France *** opinions {are only mines, sont seulement les miennes} *** ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] different records, same field name? 2008-03-03 16:55 ` Basile STARYNKEVITCH @ 2008-03-03 19:32 ` Kuba Ober 2008-03-03 21:04 ` Michael Wohlwend 2008-03-03 22:29 ` Jon Harrop 0 siblings, 2 replies; 9+ messages in thread From: Kuba Ober @ 2008-03-03 19:32 UTC (permalink / raw) To: caml-list On Monday 03 March 2008, Basile STARYNKEVITCH wrote: > mfmorss@aep.com wrote: > > In any case, is it possible to define and use different types of records, > > which nevertheless share some field names? I have had difficulty doing > > this in the toplevel. > > It is not possible, for a simple reason: > > Suppose you have two record type tr1 & tr2 sharing a common field name f > > type tr1 = { f : int; f1 : string } > > type tr2 = { f2 : float; f: int } > > Then the compiler won't be able to infer the type of function ff > retrieving this field f > > let ff x = x.f > > So the requirement that each field name is different is the price to pay > to have type inference. I think that type inference in OCaml sometimes gets in the way of other language features. I'd much rather see OCaml go in a direction where type inference is performed on "as far as it can go" basis. In the cited example, if the compiler couldn't infer the type, it should simply remind the user to use explicit type as appropriate in such case. While I do admit that this may cause some "silly" behavior, where changes in unrelated code can cause compile errors elsewhere, this should be naturally shielded by module boundaries. So, as far as I can see, it wouldn't suddenly break precompiled modules, at least. Cheers, Kuba ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] different records, same field name? 2008-03-03 19:32 ` Kuba Ober @ 2008-03-03 21:04 ` Michael Wohlwend 2008-03-03 22:29 ` Jon Harrop 1 sibling, 0 replies; 9+ messages in thread From: Michael Wohlwend @ 2008-03-03 21:04 UTC (permalink / raw) To: caml-list Am Montag 03 März 2008 20:32:02 schrieb Kuba Ober: > > I think that type inference in OCaml sometimes gets in the way of other > language features. I'd much rather see OCaml go in a direction where type > inference is performed on "as far as it can go" basis. I'm also the opinion, that type inference should be a feature. In the case of the record labels, if the compiler cannot decide the type of a record, an error could be issued to let the user manually fix it. The duplicate record label thingy arises regulary on the list, but I think it will stay a feature of ocaml :-) Michael ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] different records, same field name? 2008-03-03 19:32 ` Kuba Ober 2008-03-03 21:04 ` Michael Wohlwend @ 2008-03-03 22:29 ` Jon Harrop 1 sibling, 0 replies; 9+ messages in thread From: Jon Harrop @ 2008-03-03 22:29 UTC (permalink / raw) To: caml-list On Monday 03 March 2008 19:32:02 Kuba Ober wrote: > I think that type inference in OCaml sometimes gets in the way of other > language features. I'd much rather see OCaml go in a direction where type > inference is performed on "as far as it can go" basis. Other languages already do that, of course, and it just brings a different set of trade-offs. OCaml is roughly the best you can get in this respect. Breaking decideability seems like a bad idea because you end up annotating all types and benefitting much less from type inference, as is idiomatic in Haskell and imposed in Scala. The alternative of flagging an error when an ambiguity arises is used by F# but it makes no attempt to search current definitions for structurally- compatible classes. Indeed, I already find it confusing and a little ugly to have to add type annotations as you develop because your final product often ends up with unnecessary annotations as later developments remove ambiguities. -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. http://www.ffconsultancy.com/products/?e ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] different records, same field name? 2008-03-03 16:51 different records, same field name? mfmorss 2008-03-03 16:53 ` [Caml-list] " Jon Harrop 2008-03-03 16:55 ` Basile STARYNKEVITCH @ 2008-03-03 18:45 ` DooMeeR 2008-03-04 9:52 ` Berke Durak 2 siblings, 1 reply; 9+ messages in thread From: DooMeeR @ 2008-03-03 18:45 UTC (permalink / raw) To: mfmorss; +Cc: caml-list caml-list mfmorss@aep.com a écrit : > I have not used OCaml, just done some reading about it and toyed with the > toplevel to see if it might be a useful tool here. I have "Practical > OCaml," which unfortunately is a rather wretched reference. > > In any case, is it possible to define and use different types of records, > which nevertheless share some field names? I have had difficulty doing > this in the toplevel. Sharing names for record labels, as well as for variant tags, is not a good idea. Indeed, only the last name is remembered (as for everything in OCaml), so the type system will infer the last type which has this label. One way to separate names is to use modules, for instance: module People = struct type t = { name: string; age: int; } end;; Then you can use labels People.name and People.age, for instance: let x = { People.name = "x"; age = 69; };; Notice how, when defining this variant, you don't have to use "People." for every label. x.People.name The toplevel gives you: - : string = "x" If you know you won't have any name clash, you can open module People and you won't have to use "People." everytime: open People;; let x = { name = "x"; age = 69; };; -- Romain Bardou ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] different records, same field name? 2008-03-03 18:45 ` DooMeeR @ 2008-03-04 9:52 ` Berke Durak 2008-03-04 10:25 ` David Teller 0 siblings, 1 reply; 9+ messages in thread From: Berke Durak @ 2008-03-04 9:52 UTC (permalink / raw) To: DooMeeR; +Cc: mfmorss, caml-list caml-list DooMeeR a écrit : > mfmorss@aep.com a écrit : >> I have not used OCaml, just done some reading about it and toyed with >> the toplevel to see if it might be a useful tool here. I have >> "Practical OCaml," which unfortunately is a rather wretched reference. >> >> In any case, is it possible to define and use different types of >> records, which nevertheless share some field names? I have had >> difficulty doing this in the toplevel. > > Sharing names for record labels, as well as for variant tags, is not a > good idea. Indeed, only the last name is remembered (as for everything > in OCaml), so the type system will infer the last type which has this > label. > > One way to separate names is to use modules, for instance: > > module People = struct > type t = { > name: string; > age: int; > } > end;; > > Then you can use labels People.name and People.age, for instance: > > let x = { > People.name = "x"; > age = 69; > };; > > Notice how, when defining this variant, you don't have to use "People." > for every label. > > x.People.name > > The toplevel gives you: > - : string = "x" > > If you know you won't have any name clash, you can open module People > and you won't have to use "People." everytime: > > open People;; > > let x = { > name = "x"; > age = 69; > };; > That's one instance were Alain's openin extension shows its usefulness. http://alain.frisch.fr/soft.html#openin let sprint_time () t = open Unix in sprintf "%04d-%02d-%02d" t.tm_year t.(tm_mon+1) t.tm_day Here's one thing we could do as part of the OSR. Define a list of "standard" syntax extensions and libraries. I would certainly vote for openin to be included by default. Then let the fine Debian Ocaml maintainers write a wrapper scripts around the ocaml compiler that will use those extensions and have those libraries accessible by default; name the resulting binaries in a consistent way, such as by appending an "s" (for "standard") at the end of command names, as in "ocamlcs", "ocamlopts". Put those in a package called "ocaml-osr". Tell people new to Ocaml to use those instead; the existing users will be told to just add an "s". ocamlcs, ocamlopts would expand to ocamlc -I +pcre -I +lablgtk -I +extlib -I +whatever... -pp camlp4o -pp pa_openin .. possibly depending on installed packages. -- Berke DURAK ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] different records, same field name? 2008-03-04 9:52 ` Berke Durak @ 2008-03-04 10:25 ` David Teller 0 siblings, 0 replies; 9+ messages in thread From: David Teller @ 2008-03-04 10:25 UTC (permalink / raw) To: Berke Durak; +Cc: DooMeeR, caml-list caml-list On Tue, 2008-03-04 at 10:52 +0100, Berke Durak wrote: > Here's one thing we could do as part of the OSR. Define a list of > "standard" syntax extensions and libraries. > > I would certainly vote for openin to be included by default. Agreed on both accounts. Do you wish to launch a thread on that subject ? Cheers, David -- David Teller Security of Distributed Systems http://www.univ-orleans.fr/lifo/Members/David.Teller Angry researcher: French Universities need reforms, but the LRU act brings liquidations. ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2008-03-04 10:25 UTC | newest] Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2008-03-03 16:51 different records, same field name? mfmorss 2008-03-03 16:53 ` [Caml-list] " Jon Harrop 2008-03-03 16:55 ` Basile STARYNKEVITCH 2008-03-03 19:32 ` Kuba Ober 2008-03-03 21:04 ` Michael Wohlwend 2008-03-03 22:29 ` Jon Harrop 2008-03-03 18:45 ` DooMeeR 2008-03-04 9:52 ` Berke Durak 2008-03-04 10:25 ` David Teller
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox