Appendix A. Troubleshooting
This appendix details many of the problems commonly encountered by new F# programmers.
Value Restriction
One problem encountered when using an F# interactive session is the value restriction, which requires types to be completely defined.
The simplest example impeded by the value restriction is the definition of a reference to an empty list:
> let x = ref [];; ----^^stdin(2,4): error: FS0030: Value restriction. Type inference has inferred the signature val x : '_a list ref Either define 'x' as a simple data term, make it a
function, or add a type constraint to instantiate the type parameters.
The empty list has the well-defined type 'a list
but a reference to the empty list does not have a well-defined type because the empty list might be replaced by a non-empty list, in which case the elements must be of a specific type, the type of which has not been defined. In this case, the value restriction protects the programmer from accidentally setting the reference to an int list
in one part of the program but a float list
in another part of the program.
This problem can always be addressed by adding a type annotation to constrain the type of the value. In this case, we might know that the list will contain int
values, so we annotate accordingly to make the F# interactive session happy:
> let x : int list ref = ref [];; val x : int list ref
The problem occurs mainly when executing parts of a program being developed in an F# interactive session.
MUTABLE ARRAY CONTENTS ...
Get F# for Scientists now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.