-
Notifications
You must be signed in to change notification settings - Fork 7
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
add newtype declarations #85
Comments
Indirection(class WidgetImpl s
(: make-widget (-> s (WidgetImpl s)))
(: widget-state (-> (WidgetImpl s) s))
(: widget? (-> a Bool)))
(newtype Widget ;The Widget (sum) type
[Widget make-widget] ;is implemented by make-widget,
[Widget? widget?] ;can be recognized by its implementation,
[Widget-state (φ (Foo s) s)]) ;and carries a "state" value Now I can export (provide (newtype-out Widget))
;;; or, equivalently:
(provide (sum Widget) Widget Widget? Widget-state) Many InstancesThis appears to be its main use in Haskell. The class system is evolving to accommodate automatic code generation, initially for run-time type checks and dynamic dispatch. The changes will effectively turn classes into an opt-in mechanism for type-directed code generation and lay the groundwork for compile-time optimizations by Before any code can be generated, the types must be analyzed. This means the types must be specified in the code or inferred automatically (see #86). With respect to code generation, each Racket data
(newtype Box
[Box box]
[Box? box?]
[unBox unbox])
(instance WidgetImpl Box
(define make-widget Box)
(define widget? Box?)
(define widget-state unBox)) or lists: (newtype List
[List list]
[List? list?]
[unList id])
(instance WidgetImpl List
(define make-widget List)
(define widget? List?)
(define widget-state unList)) |
A special notation for representing ordinary Racket data as a single-field product type.
newtype
instances are indistinguishable from their field at run time. Their main purpose is to provide a basis for code generation and future development into modularnewtype
-aware type system components.In the following example,
a
List
is any value recognized by theList?
predicate, which in this caseis bound to
list?
. At run time, instances of the type can be created withthe
List
constructor, here bound tolist
. Instances can also be matchedagainst the
List
pattern and then de-constructed further.And finally, a
List
can beunList
ed, which in this case returns theelements of the list as multiple distinct values. If
unList
does not receiveexactly one argument, or if its sole argument is not a
List?
, a run-timeerror is raised.
The Short Form
Takes 3 or 4 non-keyword arguments.
Arguments:
<T>
Defines:
type:<T>
<T>
<T>?
<T>
un<T>
The Long Form
Takes 3 to 5 keyword arguments.
Required Keyword Arguments:
<T>
<T?>
and value<TC>
and valueOptional Keyword Argument:
<TM>
<unT>
and valueDefines:
type:<T>
<TM>
or<T>
<T?>
<TC>
<unT>
More Examples
The text was updated successfully, but these errors were encountered: