-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
"strict" record types #128
Comments
Note that the existing solution is inferior to the proposed solution, specifically with regards to > const Point = $.RecordType({x: $.Number, y: $.Number});
> Point.validate({x: 1, y: '2'})
Left({value: "2", propPath: [ "y" ]})
> //However:
> const StrictPoint = $.NullaryType('StrictPoint', '',
> x => $.test([], Point, x) && Object.keys(x).length === Point.keys.length
> );
> StrictPoint.validate({x: 1, y: '2'})
Left({value: { x: 1, y: "2" }, propPath: []}) So our However, I don't think providing a I think the only way to solve this, is by providing a formal type refinement function similar to the const Point = $.RecordType({x: $.Number, y: $.Number});
const StrictPoint = $.NullaryTypeRefinement(
Point,
'StrictPoint',
x => Object.keys(x).length === Point.keys.length
); The |
Rather than define // Integer :: Type
const Integer = $.NullaryType(
'my-package/Integer',
'http://example.com/my-package#Integer',
- x => typeof x === 'number' &&
+ [$.Number],
+ x => Math.floor(x) === x &&
x >= Number.MIN_SAFE_INTEGER &&
x <= Number.MAX_SAFE_INTEGER
);
// NonZeroInteger :: Type
const NonZeroInteger = $.NullaryType(
'my-package/NonZeroInteger',
'http://example.com/my-package#NonZeroInteger',
- x => $.test([], Integer, x) && x !== 0
+ [Integer],
+ x => x !== 0
); If you're interested in working on this change, @Avaq, I will leave it to you. Otherwise I will get to it myself at some point. |
Does it make sense to add this parameter only to |
I was imagining making the same change to // Point :: Type
const Point = $.RecordType([], {x: $.FiniteNumber, y: $.FiniteNumber});
// Color :: Type
const Color = $.NullaryType(...);
// ColorPoint :: Type
const ColorPoint = $.RecordType([Point], {color: Color});
$.test([], Point, {x: 0, y: 0}); // => true
$.test([], Point, {x: 0, y: 0, color: '#000'}); // => true
$.test([], ColorPoint, {x: 0, y: 0}); // => false
$.test([], ColorPoint, {x: 0, y: 0, color: '#000'}); // => true |
Record types permit additional fields. For example:
One may wish to define a "strict" record type instead. One may do so using
$.NullaryType
:We could provide
$.StrictRecordType
, or have$.RecordType
take an additional argument to specify whether additional fields are permitted. I'm not sure we should do so, though, given the solution above.This issue serves both to document the existing solution, and to provide a place to discuss the pros and cons of supporting "strict" record types more directly.
The text was updated successfully, but these errors were encountered: