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

Type mismatch but why?

Expand Messages
  • Christian Lerrahn
    Hi, I have this snippet of code that drives me crazy. I just don t see why my types are mismatching. It s rather ugly code anyway but I couldn t think of a
    Message 1 of 5 , Jan 4, 2007
    • 0 Attachment
      Hi,
      I have this snippet of code that drives me crazy. I just don't see why
      my types are mismatching. It's rather ugly code anyway but I couldn't
      think of a nice way of putting it so far. The error I get is

      # Characters 107-578:
      ....match (ghostptcls) with
      [] -> newghostparts
      | (gp :: gplist) -> let newgp = { r = gp; v = opart.v; ... } in
      let newlist = newgp :: newghostparts in newgparticles opart
      gplist newlist..
      This expression has type Particle.properties list but is here
      used with type Particle.properties list -> Particle.properties list

      The code is

      (* creates new ghostparticles *)
      let rec newgparticles opart ghostptcls ?(newghostparts = [opart]) =
      match (ghostptcls) with
      [] -> newghostparts
      | (gp :: gplist) -> let newgp = { r = gp; v = opart.v; ... } in let
      newlist = newgp :: newghostparts in newgparticles opart gplist newlist

      (* wrapper for ghostparticles routine in Vectorop *)
      let get_ghostparticles ~particle:particle =
      let list_of_ghostparticles = Vectorop.ghostparticle_loc particle.r
      in newgparticles particle list_of_ghostparticles []

      opart should be a single particle of type Particle.properties.

      Any hints what I am doing wrong?

      TIA,
      Christian
    • Jonathan Roewen
      It s because your optional argument can t be erased, confusing the type checker. Adding an explicit type (foo list) to newghostparts, Expecting function has
      Message 2 of 5 , Jan 4, 2007
      • 0 Attachment
        It's because your optional argument can't be erased, confusing the type checker.

        Adding an explicit type (foo list) to newghostparts,

        Expecting function has type ?newghostparts:foo list -> foo list
        This argument cannot be applied without label

        Adding the label ~newghostparts to newlist in final line:

        Warning X: this optional argument cannot be erased.
        let rec newgparticles opart ghostptcls ?(newghostparts = [opart]) =
        ^^^^^^^
        val newgparticles : foo -> foo list -> ?newghostparts:foo list -> foo list =
        <fun>

        I've never quite fully understood labels. Perhaps the fully applied
        function doesn't need labels thing doesn't apply to optional arguments
        -- which seems a shame.

        Anyways, the workaround is to label the optional argument. And to
        eliminate the warning, either change the order so it's not at the end,
        or add a dummy argument to the end (unit).

        Jonathan

        On 1/5/07, Christian Lerrahn <ocaml@...> wrote:
        > Hi,
        > I have this snippet of code that drives me crazy. I just don't see why
        > my types are mismatching. It's rather ugly code anyway but I couldn't
        > think of a nice way of putting it so far. The error I get is
        >
        > # Characters 107-578:
        > ....match (ghostptcls) with
        > [] -> newghostparts
        > | (gp :: gplist) -> let newgp = { r = gp; v = opart.v; ... } in
        > let newlist = newgp :: newghostparts in newgparticles opart
        > gplist newlist..
        > This expression has type Particle.properties list but is here
        > used with type Particle.properties list -> Particle.properties list
        >
        > The code is
        >
        > (* creates new ghostparticles *)
        > let rec newgparticles opart ghostptcls ?(newghostparts = [opart]) =
        > match (ghostptcls) with
        > [] -> newghostparts
        > | (gp :: gplist) -> let newgp = { r = gp; v = opart.v; ... } in let
        > newlist = newgp :: newghostparts in newgparticles opart gplist newlist
        >
        > (* wrapper for ghostparticles routine in Vectorop *)
        > let get_ghostparticles ~particle:particle =
        > let list_of_ghostparticles = Vectorop.ghostparticle_loc particle.r
        > in newgparticles particle list_of_ghostparticles []
        >
        > opart should be a single particle of type Particle.properties.
        >
        > Any hints what I am doing wrong?
        >
        > TIA,
        > Christian
        >
        >
        > Archives up to November 11, 2006 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
        > The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
        > Attachments are banned and you're asked to be polite, avoid flames etc.
        > Yahoo! Groups Links
        >
        >
        >
        >
      • Christian Lerrahn
        Hi Jonathan, I feel stupid but I didn t understand your remedy. I tried to explicitly state the type of my optional argument but the error stayed the same.
        Message 3 of 5 , Jan 4, 2007
        • 0 Attachment
          Hi Jonathan,
          I feel stupid but I didn't understand your remedy. I tried to
          explicitly state the type of my optional argument but the error stayed
          the same. Would you mind writing out what you explained? I just seem to
          be too stupid to figure out a verbal explanation. ;-)

          Cheers,
          Christian

          Am Fri, 5 Jan 2007 16:29:42 +1300
          schrieb "Jonathan Roewen" <jonathan.roewen@...>:

          > It's because your optional argument can't be erased, confusing the
          > type checker.
          >
          > Adding an explicit type (foo list) to newghostparts,
          >
          > Expecting function has type ?newghostparts:foo list -> foo list
          > This argument cannot be applied without label
          >
          > Adding the label ~newghostparts to newlist in final line:
          >
          > Warning X: this optional argument cannot be erased.
          > let rec newgparticles opart ghostptcls ?(newghostparts = [opart]) =
          > ^^^^^^^
          > val newgparticles : foo -> foo list -> ?newghostparts:foo list -> foo
          > list = <fun>
          >
          > I've never quite fully understood labels. Perhaps the fully applied
          > function doesn't need labels thing doesn't apply to optional arguments
          > -- which seems a shame.
          >
          > Anyways, the workaround is to label the optional argument. And to
          > eliminate the warning, either change the order so it's not at the end,
          > or add a dummy argument to the end (unit).
          >
          > Jonathan
          >
          > On 1/5/07, Christian Lerrahn <ocaml@...> wrote:
          > > Hi,
          > > I have this snippet of code that drives me crazy. I just don't see
          > > why my types are mismatching. It's rather ugly code anyway but I
          > > couldn't think of a nice way of putting it so far. The error I get
          > > is
          > >
          > > # Characters 107-578:
          > > ....match (ghostptcls) with
          > > [] -> newghostparts
          > > | (gp :: gplist) -> let newgp = { r = gp; v = opart.v; ... } in
          > > let newlist = newgp :: newghostparts in newgparticles opart
          > > gplist newlist..
          > > This expression has type Particle.properties list but is here
          > > used with type Particle.properties list -> Particle.properties list
          > >
          > > The code is
          > >
          > > (* creates new ghostparticles *)
          > > let rec newgparticles opart ghostptcls ?(newghostparts = [opart]) =
          > > match (ghostptcls) with
          > > [] -> newghostparts
          > > | (gp :: gplist) -> let newgp = { r = gp; v = opart.v; ... } in
          > > let newlist = newgp :: newghostparts in newgparticles opart gplist
          > > newlist
          > >
          > > (* wrapper for ghostparticles routine in Vectorop *)
          > > let get_ghostparticles ~particle:particle =
          > > let list_of_ghostparticles = Vectorop.ghostparticle_loc
          > > particle.r in newgparticles particle list_of_ghostparticles []
          > >
          > > opart should be a single particle of type Particle.properties.
          > >
          > > Any hints what I am doing wrong?
          > >
          > > TIA,
          > > Christian
          > >
          > >
          > > Archives up to November 11, 2006 are also downloadable at
          > > http://www.connettivo.net/cntprojects/ocaml_beginners/ The archives
          > > of the very official ocaml list (the seniors' one) can be found at
          > > http://caml.inria.fr Attachments are banned and you're asked to be
          > > polite, avoid flames etc. Yahoo! Groups Links
          > >
          > >
          > >
          > >
        • Robert Roessler
          ... Just to emphasize this point, from 4.1.1 Optional arguments of the OCaml manual we have A function taking some optional arguments must also take at
          Message 4 of 5 , Jan 4, 2007
          • 0 Attachment
            Jonathan Roewen wrote:
            > ...
            > I've never quite fully understood labels. Perhaps the fully applied
            > function doesn't need labels thing doesn't apply to optional arguments
            > -- which seems a shame.
            >
            > Anyways, the workaround is to label the optional argument. And to
            > eliminate the warning, either change the order so it's not at the end,
            > or add a dummy argument to the end (unit).

            Just to emphasize this point, from "4.1.1 Optional arguments" of the
            OCaml manual we have

            "A function taking some optional arguments must also take at least one
            non-labeled argument. This is because the criterion for deciding
            whether an optional has been omitted is the application on a
            non-labeled argument appearing after this optional argument in the
            function type."

            While this may not directly address your "fully applied" issue, it
            definitely covers the ordering thing.

            Robert Roessler
            robertr@...
            http://www.rftp.com
          • Jonathan Roewen
            ... let newlist = ... in newgparticles opart gplist ~newghostparts:newlist And you ll get the warning about the optional argument not being able to be
            Message 5 of 5 , Jan 4, 2007
            • 0 Attachment
              > > > The code is
              > > >
              > > > (* creates new ghostparticles *)
              > > > let rec newgparticles opart ghostptcls ?(newghostparts = [opart]) =
              > > > match (ghostptcls) with
              > > > [] -> newghostparts
              > > > | (gp :: gplist) -> let newgp = { r = gp; v = opart.v; ... } in
              > > > let newlist = newgp :: newghostparts in newgparticles opart gplist
              > > > newlist

              let newlist = ... in
              newgparticles opart gplist ~newghostparts:newlist

              And you'll get the warning about the optional argument not being able
              to be erased...

              Jonathan
            Your message has been successfully submitted and would be delivered to recipients shortly.