8.18
top
← prev up next →

Struct type properties for Typed/RacketπŸ”— i

Suzanne Soy <racket@suzanne.soy>

syntax

( struct/props maybe-type-varsnamemaybe-parent([field:type]...)
options...)
maybe-type-vars =
| (v...)
maybe-parent =
| parent-id
options = #:transparent
| #:propertyprop:custom-write custom-write
| #:propertyprop:equal+hash equal+hash
custom-write :
(name;non-polymorphic case
Output-Port
(U#t#f01)
Any)
custom-write :
((v... );polymorphic case
((namev... )
Output-Port
(U#t#f01)
Any))
equal+hash :
(List(name;non-polymorphic case
name
(AnyAnyBoolean)
Any)
(v
(AnyInteger)
Integer)
(v
(AnyInteger)
Integer))
equal+hash :
(List((v... v2... );polymorphic case
((namev... )
(namev2... )
(AnyAnyBoolean)
Any))
((v... )
((namev... )
(AnyInteger)
Integer))
((v... )
((namev... )
(AnyInteger)
Integer)))
This form defines a typed/racket struct type, and accepts a small subset of racket’s struct type properties.

For prop:custom-write and prop:equal+hash , the function types are polymorphic only if the struct is polymorphic (i.e. maybe-type-vars is present) and has at least one type variable (i.e. maybe-type-vars is not just ()).

It implements these struct type properties in a type-safe manner: the current implementation in typed/racket does not properly type-check functions and values used as struct type properties. This library declares the user-provided functions outside of the struct definition, with the type given above (e.g. ((v... )((namev... )Output-Port(U#t#f01)Any)) for the argument of the prop:custom-write property), to ensure that these functions and values are properly checked.

The API should (hopefully) stay backward-compatible when Typed/Racket officially supports (or rejects) structure type properties. In other words:
  • If typed/racket eventually implements the same interface as the one provided by this library, then we will update this library so that it simply re-provide struct renamed as struct/props .

  • If typed/racket eventually implements some type-safe struct type properties, then we will update this library will so that it translates back to typed/racket’s implementation, as much as possible.

  • If typed/racket eventually disallows struct type properties, then we will update this library so that it uses some typed/racket/unsafe tricks to still make them available, if it can be done.

top
← prev up next →

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /