caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] type inference of record field that is an array
@ 2017-05-16 16:58 Tim Leonard
  2017-05-16 17:05 ` Ivan Gotovchits
  0 siblings, 1 reply; 2+ messages in thread
From: Tim Leonard @ 2017-05-16 16:58 UTC (permalink / raw)
  To: caml-list

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

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: 2157 bytes --]

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

* Re: [Caml-list] type inference of record field that is an array
  2017-05-16 16:58 [Caml-list] type inference of record field that is an array Tim Leonard
@ 2017-05-16 17:05 ` Ivan Gotovchits
  0 siblings, 0 replies; 2+ messages in thread
From: Ivan Gotovchits @ 2017-05-16 17:05 UTC (permalink / raw)
  To: Tim Leonard; +Cc: caml-list

[-- 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 --]

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

end of thread, other threads:[~2017-05-16 17:05 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-16 16:58 [Caml-list] type inference of record field that is an array Tim Leonard
2017-05-16 17:05 ` Ivan Gotovchits

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