Singleton: Difference between revisions
Content added Content deleted
Line 1,336: | Line 1,336: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
Singletons are not very useful in Racket, because functions that use module state are more straightforward. However, |
Singletons are not very useful in Racket, because functions that use module state are more straightforward. However, classes are first class values, and therefore they follow the same rules as all other bindings. For example, a class can be made and instantiated but not provided to client files: |
||
⚫ | |||
⚫ | |||
#lang racket |
#lang racket |
||
(provide instance) |
(provide instance) |
||
(define singleton% |
(define singleton% |
||
(class object% |
(class object% |
||
(super-new))) |
(super-new))) |
||
⚫ | |||
</lang> |
|||
Or better, not name the class at all: |
|||
⚫ | |||
<lang racket> |
|||
#lang racket |
|||
(provide instance) |
|||
(define instance |
|||
(new (class object% |
|||
(define/public (foo) 123) |
|||
(super-new)))) |
|||
</lang> |
</lang> |
||