Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to specify the type of defined values with "val" declarations #6703

Closed
vicuna opened this issue Dec 10, 2014 · 3 comments
Closed

Allow to specify the type of defined values with "val" declarations #6703

vicuna opened this issue Dec 10, 2014 · 3 comments

Comments

@vicuna
Copy link

vicuna commented Dec 10, 2014

Original bug ID: 6703
Reporter: @alainfrisch
Status: acknowledged (set by @damiendoligez on 2015-01-08T22:50:01Z)
Resolution: open
Priority: normal
Severity: feature
Target version: later
Category: typing
Tags: patch
Related to: #6688 #6698
Monitored by: @gasche @diml @ygrek @hcarty

Bug description

It would be convenient to use "val" declarations in structures to specify the type of value defined by a "let binding" immediately after:

val f : int -> int
val g : int -> int

let rec f x = ...
and g x = ...

Values defined by the let-binding and mentioned in immediately preceding val declarations will be matched against those declarations. Those not mentioned will use their inferred type (i.e. this cannot be used to hide declarations).

Such sequences of val declarations must be followed immediately by a let binding.

Notes:

  • "val" declarations are already accepted syntactically as structure items (see Accept value declarations as structure items #6688), but currently always rejected by the type-checker.

  • This is a conservative proposal: a more ambitious one could be to use "val" declarations to support forward definitions. We can do that later (or not).

  • Using "val" declarations makes it possible, and natural, to attach attributes such as @@ocaml.deprecated to inferred signature items (allow @@ocaml.deprecated on structure items? #6698).

  • I've attached a patch which implements the feature. Note that val declarations are not used to type-check the value definitions, only to check the inferred types. In particular, this form cannot be used as the explicit annotation required for polymorphic recursion. There is also a problem with location on error report: while the locations of the definition and the declaration are reported as "sub-location" in the error message, the main location points to the entire file.

File attachments

@vicuna
Copy link
Author

vicuna commented Dec 12, 2014

Comment author: @alainfrisch

With the attached patch, "val x : ... let x = ...." behaves as "include (struct let x = ... end : sig val x : ... end)" (but identifiers not listed in val declarations are not hidden). In particular, the declaration can be less general than the definition.

If we want to be able to use declarations as an alternative way to specify the annotation required for polymorphic recursion, this interpretation wouldn't work anymore. The declaration would have to be taken into account while type-checking the definition.

@vicuna
Copy link
Author

vicuna commented Dec 12, 2014

Comment author: @lpw25

If we want to be able to use declarations as an alternative way to specify the annotation required for polymorphic recursion, this interpretation wouldn't work anymore.

For me, that is the main benefit of this syntax.

@github-actions
Copy link

This issue has been open one year with no activity. Consequently, it is being marked with the "stale" label. What this means is that the issue will be automatically closed in 30 days unless more comments are added or the "stale" label is removed. Comments that provide new information on the issue are especially welcome: is it still reproducible? did it appear in other contexts? how critical is it? etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant