-
Notifications
You must be signed in to change notification settings - Fork 20
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 Getters and Setters for Primitive Types To Validate and Coerce Values #517
Comments
I'm a little bit confused as to the goal with this. Is the primitive to avoid the overhead of a record? Would a single case struct union that can only be constructed via an exposed function fill a similar role? |
This should be done with a type class not as a language feature |
I agree with this comment. There are ways of defining new types in F# that have enforced invariants, e.g. a simple class or struct type that checks on creation. That's the way to do this today. |
Many thanks for the suggestion. We won't be adding this new form of type definition to F#. Our recommendation is to use a new struct, union, record or class definition that enforces the invariant of the new type. |
Allow Getters and Setters for Primitive Types To Validate and Coerce Values
I suggest adding support for getters and setters for primitive type extensions and user-defined types extending primitive types in order to improve F#'s ability to make invalid states unrepresentable at the code level.
This suggestion is an extention of suggestion #516 to primitive types.
Syntax for a user-defined type extending a primitive type:
Syntax for a primitive type extension:
A language user is under no obligation to implement both a getter and a setter for the Value property. A language user can define the Value property with only getter or setter. It's up to a language user to define which logic (data validation or data coercion) contained in a getter or a setter.
For example,
The get would be called each time a value is accessed, i.e.
The set would be called during value construction, copy, or assignment to a mutable named value, i.e.
Pros and Cons
The advantages of making this adjustment to F# are:
The disadvantages of making this adjustment to F# are:
Extra information
Estimated cost (XS, S, M, L, XL, XXL): L (like in suggustion #516).
Related suggestions: Suggestion #516
Affadavit (must be submitted)
Please tick this by placing a cross in the box:
Please tick all that apply:
The text was updated successfully, but these errors were encountered: