[racket] (values) vs (void)

From: Gustavo Massaccesi (gustavo at oma.org.ar)
Date: Mon Nov 24 11:58:00 EST 2014

The result '(void)' is '#<void>'. So 'void' is a usual function that
returns 1 value. (Note: You cant just type in a program, because the
reader doesn't recognize '#<void>'.)

In a form in the main part of a module the returned values are
printed. The trick is that 'current-print' prints everything that is
not '#void'. So even if 'void' return a value, the value is not
printed on the screen. On the other hand (values) returns 0 values, so
'current-print' is never called. You can see the differences in this
example.

(As other had said, always use (void) unless you have a very special case.)

Gustavo

;------
#lang racket

; display everything
(current-print displayln)
(define x 5)
(+ 1 2)
(void)
(values)
(values 8 9)

; display everything, inside brackets
(current-print (lambda (x) (display (format "[~a]\n" x))))
(define y 5)
(+ 1 2)
(void)
(values)
(values 8 9)

; "standard" display
(current-print (lambda (x) (unless (void? x) (displayln x))))
(define z 5)
(+ 1 2)
(void)
(values)
(values 8 9)
;-----


On Sun, Nov 23, 2014 at 3:31 PM, Roman Klochkov <kalimehtar at mail.ru> wrote:
>> void is a good placeholder in dummy functions/objects
>
> (define (foo) (values)) also works just fine.
>
>> fill a gap, which is useful
>
> If function returns void?, then it is used as a procedure. I mean returned
> value never assigned to a variable.
> So in what cases it is useful?
>
>
> Sun, 23 Nov 2014 13:20:26 +0000 от Stephen De Gabrielle
> <spdegabrielle at gmail.com>:
>
> void is a good placeholder in dummy functions/objects. It doesn't do
> anything, except for fill a gap, which is useful.
>
> Values is used to pass multiple values.
>
> Check the manual for details.
>
> Does that help?
>
> Stephen
>
> On Sun, 23 Nov 2014 at 12:56 Roman Klochkov <kalimehtar at mail.ru> wrote:
>
> When I should use (void) and when (values)?
>
> They are both used to show, that there are no return values.
> They are both not printable.
>
> What is intended use for (void) and (values) and when one should prefer one
> over other?
>
> --
> Roman Klochkov
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>
>
>
> --
> Roman Klochkov
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>


Posted on the users mailing list.