Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0007420OCamltypingpublic2016-11-25 15:562017-02-19 18:27
Assigned To 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version 
Summary0007420: Confusing syntax for labelled and optional argument type annotations
DescriptionType annotations on labelled or optional arguments in .ml files are thrown away without warning:

# let f ?x:int y = y;;
val f : ?x:'a -> 'b -> 'b = <fun>

# let f ~x:int y = y;;
val f : x:'a -> 'b -> 'b = <fun>

The labelled one should be used in the normal way, to restrict the type and the optional one should be rejected, not being an option type, surely? It seems odd to silently drop them.

Additional InformationFor reference, for anyone as easily confused as me, here is a .mli/.ml pair which typechecks for such a function

val f : ?x:int -> 'a -> 'a

let f ?(x : int option) y = y

The option-ness of the type of 'x' is implicit in the .mli, explicit in the .ml.
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
johnwhitington (reporter)
2016-11-25 15:58

The first definition is properly rejected by ocamlc -i:

let f ?x:int y = y;;

$ ocamlc -i
File "", line 1, characters 8-13:
Error: This pattern matches values of type int
       but a pattern was expected which matches values of type 'a option

So perhaps this is just a top-level issue.
lpw25 (developer)
2016-11-25 16:14
edited on: 2016-11-25 16:14

This is merely a confusion of syntax: you're actually specifying the name of the parameter with that syntax rather than its type. For example,

  let f ~x:int y = int;;
  val f : x:'a -> 'b -> 'a = <fun>

To specify a type you should use:

  # let f ~(x:int) y = y;;
  val f : x:int -> 'a -> 'a = <fun>

It's an unfortunate similarity, but I don't think anything can be done at this point.

johnwhitington (reporter)
2016-11-25 16:20

Of course. Thanks.
gasche (administrator)
2016-11-25 17:04

> It's an unfortunate similarity, but I don't think anything can be done at this point.

That sounds awkward, but maybe we could warn if the identifier chosen happens to be one of the built-in non-parameterized types of the language? (I don't think that `int`, `char` or `bool` are commendable identifier names anyway.)
xleroy (administrator)
2017-02-19 18:27

Suspending this PR by lack of ideas on what could be done.

- Issue History
Date Modified Username Field Change
2016-11-25 15:56 johnwhitington New Issue
2016-11-25 15:58 johnwhitington Note Added: 0016604
2016-11-25 16:14 lpw25 Note Added: 0016605
2016-11-25 16:14 lpw25 Note Edited: 0016605 View Revisions
2016-11-25 16:20 johnwhitington Note Added: 0016606
2016-11-25 17:04 gasche Note Added: 0016607
2016-11-25 17:05 gasche Status new => feedback
2016-12-07 18:16 shinwell Category OCaml general => OCaml typing
2017-02-19 18:27 xleroy Note Added: 0017353
2017-02-19 18:27 xleroy Status feedback => resolved
2017-02-19 18:27 xleroy Resolution open => suspended
2017-02-19 18:27 xleroy Summary Labelled and optional argument type annotations ignored => Confusing syntax for labelled and optional argument type annotations
2017-02-23 16:45 doligez Category OCaml typing => typing

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker