Define a primitive data type: Difference between revisions
Content added Content deleted
m (omissions) |
(Add Racket entry) |
||
Line 1,056: | Line 1,056: | ||
<class '__main__.num'> |
<class '__main__.num'> |
||
>>></lang> |
>>></lang> |
||
=={{header|Racket}}== |
|||
Most Racket programmers will use contracts to enforce additional guarantees on values. In the following example, the program exports <tt>x</tt> with a contract that ensures it is a number between 1 and 10. |
|||
<lang racket> |
|||
#lang racket |
|||
(provide (contract-out [x 1-to-10/c])) |
|||
(define 1-to-10/c (between/c 1 10)) |
|||
(define x 5) |
|||
</lang> |
|||
In Typed Racket, it is possible to define a type that only contains the integers from 1 to 10. However, this type is inconvenient to use and is unlikely to be used in practice. |
|||
<lang racket> |
|||
#lang typed/racket |
|||
(define-type 1UpTo10 (U 1 2 3 4 5 6 7 8 9 10)) |
|||
;; type-checks |
|||
(: x 1UpTo10) |
|||
(define x 3) |
|||
;; does not type-check |
|||
(: y 1UpTo10) |
|||
(define y 18) |
|||
</lang> |
|||
=={{header|Retro}}== |
=={{header|Retro}}== |