Loading ...
Sorry, an error occurred while loading the content.

Records, Lists and Variant Types

Expand Messages
  • Chris Yocum
    Hi Guys, Ok, I have a number of record types and I wanted to be able to put them in a list together. So I figured I would do something like this: type rec1 =
    Message 1 of 5 , Dec 27, 2010
    • 0 Attachment
      Hi Guys,

      Ok, I have a number of record types and I wanted to be able to put them
      in a list together. So I figured I would do something like this:

      type rec1 = {...}
      type rec2 = {...}
      type rec3 = {...}

      type recs = | Foo of rec1
      | Bar of rec2
      | Baz of rec3

      It works but I was wondering if anyone had any ideas on a better way of
      doing this?

      Chris



      [Non-text portions of this message have been removed]
    • Philippe Veber
      2010/12/27 Chris Yocum ... AFAIK, there are three ways to have values of different types coexist in a single structure : - union types (as
      Message 2 of 5 , Dec 27, 2010
      • 0 Attachment
        2010/12/27 Chris Yocum <cyocum@...>

        > Hi Guys,
        >
        > Ok, I have a number of record types and I wanted to be able to put them
        > in a list together. So I figured I would do something like this:
        >
        > type rec1 = {...}
        > type rec2 = {...}
        > type rec3 = {...}
        >
        > type recs = | Foo of rec1
        > | Bar of rec2
        > | Baz of rec3
        >
        > It works but I was wondering if anyone had any ideas on a better way of
        > doing this?
        >
        > Chris
        >

        AFAIK, there are three ways to have values of different types coexist in a
        single structure :
        - union types (as you proposed), think also of polymorphic variants for this
        task, which are more lightweight sometimes
        - object type system, if your three types are objects (instead of records)
        and can be coerced to a useful supertype (but then you lose some information
        on your values)
        - universal types (see http://ocaml.janestreet.com/?q=node/18)

        hth,
        Philippe.


        [Non-text portions of this message have been removed]
      • Chris Yocum
        Thanks Philippe, I had thought of using the object type system and I was strongly for doing that but at the same time I was trying to keep things functional .
        Message 3 of 5 , Dec 27, 2010
        • 0 Attachment
          Thanks Philippe,

          I had thought of using the object type system and I was strongly for
          doing that but at the same time I was trying to keep things "functional".

          Thanks!,
          Chris

          On 27/12/10 11:36, Philippe Veber wrote:
          > 2010/12/27 Chris Yocum <cyocum@...>
          >
          >> Hi Guys,
          >>
          >> Ok, I have a number of record types and I wanted to be able to put them
          >> in a list together. So I figured I would do something like this:
          >>
          >> type rec1 = {...}
          >> type rec2 = {...}
          >> type rec3 = {...}
          >>
          >> type recs = | Foo of rec1
          >> | Bar of rec2
          >> | Baz of rec3
          >>
          >> It works but I was wondering if anyone had any ideas on a better way of
          >> doing this?
          >>
          >> Chris
          >>
          >
          > AFAIK, there are three ways to have values of different types coexist in a
          > single structure :
          > - union types (as you proposed), think also of polymorphic variants for this
          > task, which are more lightweight sometimes
          > - object type system, if your three types are objects (instead of records)
          > and can be coerced to a useful supertype (but then you lose some information
          > on your values)
          > - universal types (see http://ocaml.janestreet.com/?q=node/18)
          >
          > hth,
          > Philippe.
          >
          >
          > [Non-text portions of this message have been removed]
          >
          >



          [Non-text portions of this message have been removed]
        • oliver@first.in-berlin.de
          I would prefer your first solution with the sum-type and, not the OO-way, if not certain reasons would push me in that direction. Why do you want to avoid the
          Message 4 of 5 , Dec 27, 2010
          • 0 Attachment
            I would prefer your first solution with the sum-type
            and, not the OO-way, if not certain reasons would
            push me in that direction.

            Why do you want to avoid the sum-types?
            What is your main interest? Putting things togeteher,
            or the ability to seperate them later? Or to get a quick solution? Or...


            Ciao,
            Oliver


            On Mon, Dec 27, 2010 at 12:55:27PM +0000, Chris Yocum wrote:
            > Thanks Philippe,
            >
            > I had thought of using the object type system and I was strongly for
            > doing that but at the same time I was trying to keep things "functional".
            >
            > Thanks!,
            > Chris
            >
            > On 27/12/10 11:36, Philippe Veber wrote:
            > > 2010/12/27 Chris Yocum <cyocum@...>
            > >
            > >> Hi Guys,
            > >>
            > >> Ok, I have a number of record types and I wanted to be able to put them
            > >> in a list together. So I figured I would do something like this:
            > >>
            > >> type rec1 = {...}
            > >> type rec2 = {...}
            > >> type rec3 = {...}
            > >>
            > >> type recs = | Foo of rec1
            > >> | Bar of rec2
            > >> | Baz of rec3
            > >>
            > >> It works but I was wondering if anyone had any ideas on a better way of
            > >> doing this?
            > >>
            > >> Chris
            > >>
            > >
            > > AFAIK, there are three ways to have values of different types coexist in a
            > > single structure :
            > > - union types (as you proposed), think also of polymorphic variants for this
            > > task, which are more lightweight sometimes
            > > - object type system, if your three types are objects (instead of records)
            > > and can be coerced to a useful supertype (but then you lose some information
            > > on your values)
            > > - universal types (see http://ocaml.janestreet.com/?q=node/18)
            > >
            > > hth,
            > > Philippe.
            > >
            > >
            > > [Non-text portions of this message have been removed]
            > >
            > >
            >
            >
            >
            > [Non-text portions of this message have been removed]
            >
          • Chris Yocum
            Well, the data is hierarchical which allows for an OO method but there is not enough computation happening on the classes to justify a full OO solution.
            Message 5 of 5 , Dec 27, 2010
            • 0 Attachment
              Well, the data is hierarchical which allows for an OO method but there
              is not enough computation happening on the classes to justify a full OO
              solution. Basically, the objects would just be records with inheritance
              which pushes me towards the sum-type solution.

              My motivation is to be as strictly typed as possible. I hope this makes
              sense.

              I kind of wish there was a record with inheritance do I don't have to do
              this:

              type parent_rec = { ... }

              type child_rec = { parent : parent_rec }

              Although it does allow one to do this:

              type A = { ... }
              type B = { ... }

              type 'a child_rec { parent : 'a }

              so that you can do this:

              let blah = {parent : {...}; ...}

              Chris

              On 27/12/10 14:18, oliver@...-berlin.de wrote:
              >
              >
              > I would prefer your first solution with the sum-type
              > and, not the OO-way, if not certain reasons would
              > push me in that direction.
              >
              > Why do you want to avoid the sum-types?
              > What is your main interest? Putting things togeteher,
              > or the ability to seperate them later? Or to get a quick solution? Or...
              >
              > Ciao,
              > Oliver
              >
              > On Mon, Dec 27, 2010 at 12:55:27PM +0000, Chris Yocum wrote:
              >> Thanks Philippe,
              >>
              >> I had thought of using the object type system and I was strongly for
              >> doing that but at the same time I was trying to keep things "functional".
              >>
              >> Thanks!,
              >> Chris
              >>
              >> On 27/12/10 11:36, Philippe Veber wrote:
              >> > 2010/12/27 Chris Yocum <cyocum@... <mailto:cyocum%40gmail.com>>
              >> >
              >> >> Hi Guys,
              >> >>
              >> >> Ok, I have a number of record types and I wanted to be able to put them
              >> >> in a list together. So I figured I would do something like this:
              >> >>
              >> >> type rec1 = {...}
              >> >> type rec2 = {...}
              >> >> type rec3 = {...}
              >> >>
              >> >> type recs = | Foo of rec1
              >> >> | Bar of rec2
              >> >> | Baz of rec3
              >> >>
              >> >> It works but I was wondering if anyone had any ideas on a better way of
              >> >> doing this?
              >> >>
              >> >> Chris
              >> >>
              >> >
              >> > AFAIK, there are three ways to have values of different types
              > coexist in a
              >> > single structure :
              >> > - union types (as you proposed), think also of polymorphic variants
              > for this
              >> > task, which are more lightweight sometimes
              >> > - object type system, if your three types are objects (instead of
              > records)
              >> > and can be coerced to a useful supertype (but then you lose some
              > information
              >> > on your values)
              >> > - universal types (see http://ocaml.janestreet.com/?q=node/18)
              >> >
              >> > hth,
              >> > Philippe.
              >> >
              >> >
              >> > [Non-text portions of this message have been removed]
              >> >
              >> >
              >>
              >>
              >>
              >> [Non-text portions of this message have been removed]
              >>
              >
              >



              [Non-text portions of this message have been removed]
            Your message has been successfully submitted and would be delivered to recipients shortly.