caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Ivan Gotovchits <ivg@ieee.org>
To: Tim Leonard <Tim@timleonard.us>
Cc: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] type inference of record field that is an array
Date: Tue, 16 May 2017 13:05:03 -0400	[thread overview]
Message-ID: <CALdWJ+yxgDhDk0dGWeuSmnsPK23JeKdKRP-rt78FwWZHXcq0ug@mail.gmail.com> (raw)
In-Reply-To: <C370B8CA-66DD-4635-A26D-AF12126C5552@TimLeonard.us>

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

Hi Tim,

The `x.[i] <- v` syntax maps to `String.set` function, that has type
`string -> char -> unit`, the array subscription syntax uses parentheses
instead of the brackets. So in your case, it should be

    x.array_field.(1) <- 3


If you are concerned with the warning, that is emitted before the error.
Then this is expected, as `x.[i] <- v` is a syntactic sugar to the
`String.set` and it is translated on the lexing level. Thus the type
checker never sees the array/string/bigarray subscript operators (as they
are not present in the AST)

Regards,
Ivan

On Tue, May 16, 2017 at 12:58 PM, Tim Leonard <Tim@timleonard.us> wrote:

> Is type inference working as expected here, or is this a bug?
>
> # type record_type = { array_field : int array };;
> type record_type = { array_field : int array; }
> # let test_function_1 x = Array.set x.array_field 1 3;;
> val test_function_1 : record_type -> unit = <fun>
> # let test_function_2 x = x.array_field.[1] <- 3;;
> Characters 24-46:
>   let test_function_2 x = x.array_field.[1] <- 3;;
>                           ^^^^^^^^^^^^^^^^^^^^^^
> Warning 3: deprecated: String.set
> Use Bytes.set instead.
> Characters 24-37:
>   let test_function_2 x = x.array_field.[1] <- 3;;
>                           ^^^^^^^^^^^^^
> Error: This expression has type int array
>        but an expression was expected of type bytes = string
> #
>

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

      reply	other threads:[~2017-05-16 17:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-16 16:58 Tim Leonard
2017-05-16 17:05 ` Ivan Gotovchits [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CALdWJ+yxgDhDk0dGWeuSmnsPK23JeKdKRP-rt78FwWZHXcq0ug@mail.gmail.com \
    --to=ivg@ieee.org \
    --cc=Tim@timleonard.us \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).