MULTIPLE-VALUE-BIND question 
Author Message
 MULTIPLE-VALUE-BIND question

Hi,

I'm wondering why MULTIPLE-VALUE-BIND doesn't allow you to specify
default values.  This change wouldn't even interfere with the current
definition.  So, for example, if you wanted to do:

(multiple-value-bind (x (y 0)) (compute-1-or-maybe-2-values)
  ...)

I don't know how I'd treat this for the case where subsequent bindings
take on deafult values which depend on previous bindings, e.g.:

(multiple-value-bind* (x (y (1+ x))) (compute-1-or-maybe-2-values)
  ...)

I don't know if this feature would even be worth naming another macro
for, or if the current MULTIPLE-VALUE-BIND would just behave like the
2nd above.  But default values for non-returned values would be useful
at times.

I would find such a feature to be moderately valuable, and since it
would not slow down the current implementation of MULTIPLE-VALUE-BIND
when the bindings are simple (since all the defaulting behavior is
known at compile-time), I think this is something I'd recommend
changing if that were possible.

Of course, it's easy to write your own macro to do this, but because
it deals with multiple values, I have a feeling that this could be
done more efficiently by the compiler implementor.

dave



Sat, 29 Nov 2003 14:04:42 GMT  
 MULTIPLE-VALUE-BIND question

Quote:

> Hi,

> I'm wondering why MULTIPLE-VALUE-BIND doesn't allow you to specify
> default values.  This change wouldn't even interfere with the current
> definition.  So, for example, if you wanted to do:

> (multiple-value-bind (x (y 0)) (compute-1-or-maybe-2-values)
>   ...)

Could you please provide an example because where this would be really
necessary? I'd write COMPUTE-1-OR-MAYBE-2-VALUES to always return 2
values (instead of returning only one value return two values, the
second being the default).

--
Janis Dzerins

  If million people say a stupid thing it's still a stupid thing.



Sat, 29 Nov 2003 18:09:16 GMT  
 MULTIPLE-VALUE-BIND question

Quote:

> Could you please provide an example because where this would be really
> necessary? I'd write COMPUTE-1-OR-MAYBE-2-VALUES to always return 2
> values (instead of returning only one value return two values, the
> second being the default).

Well, you don't always have control over the number of values - you
may be calling code you didn't write.  Then you have the whole issue
of knowing if it returned a 2nd value of NIL or no 2nd value.

But in fact we can do this in a terribly general way:

(multiple-value-call #'(lambda (x &optional (y nil yp))
                         ...)
 ...)

which does much more than David asked for, I think.  So
MULTIPLE-VALUE-BIND can probably be compiled very efficiently while
MULTIPLE-VALUE-VALUE-CALL is terribly general.  I think that's quite a
good mixture.

--tim



Sat, 29 Nov 2003 18:33:46 GMT  
 MULTIPLE-VALUE-BIND question

Quote:

> Well, you don't always have control over the number of values - you
> may be calling code you didn't write.

If you call functions you didn't write, and without knowing what it
returns, I think you lose regardless of M-V-BIND's abilities.

--
Frode Vatvedt Fjeld



Sat, 29 Nov 2003 18:55:45 GMT  
 MULTIPLE-VALUE-BIND question


Quote:
>Hi,

>I'm wondering why MULTIPLE-VALUE-BIND doesn't allow you to specify
>default values.  This change wouldn't even interfere with the current
>definition.  So, for example, if you wanted to do:

>(multiple-value-bind (x (y 0)) (compute-1-or-maybe-2-values)
>  ...)

If you need this you can do it with M-V-CALL:

(multiple-value-call
  (lambda (x &optional (y 0))
    ...)
  (compute-1-or-maybe-2-values))

Most of the time when a function returns a variable number of values, it's
because the later values are irrelevant due to the earlier values.  If the
function were defined to return that optional value all the time, it
probably would return NIL in these cases, which is what M-V-BIND will bind
the variable to when no value is returned.

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.



Sat, 29 Nov 2003 22:17:01 GMT  
 MULTIPLE-VALUE-BIND question

Quote:

> If you call functions you didn't write, and without knowing what it
> returns, I think you lose regardless of M-V-BIND's abilities.

You may know it returns either one or two values, but not be able to
change it so it always returns two. Sure, you can write a wrapper, but
writing this wrapper requires you to solve the problem you already
have.

--tim



Sat, 29 Nov 2003 23:37:58 GMT  
 MULTIPLE-VALUE-BIND question

Quote:

> You may know it returns either one or two values, but not be able to
> change it so it always returns two. Sure, you can write a wrapper,
> but writing this wrapper requires you to solve the problem you
> already have.

I fail to see why you would require some function to return two
values. Do you have an example?

--
Frode Vatvedt Fjeld



Sun, 30 Nov 2003 00:35:51 GMT  
 MULTIPLE-VALUE-BIND question


Quote:

>> You may know it returns either one or two values, but not be able to
>> change it so it always returns two. Sure, you can write a wrapper,
>> but writing this wrapper requires you to solve the problem you
>> already have.

>I fail to see why you would require some function to return two
>values. Do you have an example?

The very first response in the thread suggested that instead of having the
caller do the defaulting in M-V-BIND, the function should return the
default value as its second value, rather than only returning one value.

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.



Sun, 30 Nov 2003 00:45:23 GMT  
 MULTIPLE-VALUE-BIND question

Quote:

> > You may know it returns either one or two values, but not be able to
> > change it so it always returns two. Sure, you can write a wrapper,
> > but writing this wrapper requires you to solve the problem you
> > already have.

> I fail to see why you would require some function to return two
> values. Do you have an example?

GETHASH

--
Janis Dzerins

  If million people say a stupid thing it's still a stupid thing.



Sun, 30 Nov 2003 01:25:28 GMT  
 MULTIPLE-VALUE-BIND question

Quote:
> I fail to see why you would require some function to return two
> values. Do you have an example?

> GETHASH

Bad choice of wording on my part, I guess. I was not requesting
examples of functions that need to return two values (I'm certainly
not arguing against multiple values in general). Rather, given some
function known to return either one or two values, why would you need
to somehow coerce it to always return two values? What sort of
function would this be?

--
Frode Vatvedt Fjeld



Sun, 30 Nov 2003 01:53:24 GMT  
 MULTIPLE-VALUE-BIND question

Quote:

> I fail to see why you would require some function to return two
> values. Do you have an example?

The point is that I want to default the second value, and I want to be
able to do this when NIL is one of the possible second values.  This
is the same as for things like GETHASH - you need to be able to
distinguish between a value that was NIL and no value.

--tim



Sun, 30 Nov 2003 02:29:38 GMT  
 MULTIPLE-VALUE-BIND question

Quote:

>> I fail to see why you would require some function to return two
>> values. Do you have an example?

>The point is that I want to default the second value, and I want to be
>able to do this when NIL is one of the possible second values.  This
>is the same as for things like GETHASH - you need to be able to
>distinguish between a value that was NIL and no value.

Presumably this came up because you actually ran into a function that
returns an optional second value, and you needed to assign something other
than NIL in the M-V-BIND.  Would you care to tell us what this function is
and why NIL is not an appropriate default value?

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.



Sun, 30 Nov 2003 02:35:09 GMT  
 MULTIPLE-VALUE-BIND question

Quote:


> > I fail to see why you would require some function to return two
> > values. Do you have an example?


> > GETHASH

> Bad choice of wording on my part, I guess. I was not requesting
> examples of functions that need to return two values (I'm certainly
> not arguing against multiple values in general). Rather, given some
> function known to return either one or two values, why would you need
> to somehow coerce it to always return two values? What sort of
> function would this be?

I think the conventional reason is because you imagine you are programming
in Scheme and you think return is the mirror of function call and that
if you can get wrong-number-of-args in function call, you might get that
in return, too.  Certainly, if you use MULTIMPLE-VALUE-CALL with a fixed-arity
function this would be so.  But in Scheme it is often painful to manage
optional args (there being no portable syntax for doing that; only for
doing rest args), so the pressure is on fixed-arity things by people with
a formalist enough background to want to manage multiple values.  In CL,
though, we'd just do multiple-value-call on something with optional args
and really not care.  And almost no one does multiple-value-call anyway.


Sun, 30 Nov 2003 02:34:43 GMT  
 MULTIPLE-VALUE-BIND question

Quote:

> Presumably this came up because you actually ran into a function that
> returns an optional second value, and you needed to assign something other
> than NIL in the M-V-BIND.  Would you care to tell us what this function is
> and why NIL is not an appropriate default value?

No, I was just trying to follow what the original poster (seemed to)
want.  There doesn't seem to be any purpose to being able to default
in MVB otherwise.

--tim



Sun, 30 Nov 2003 03:02:16 GMT  
 MULTIPLE-VALUE-BIND question

Quote:

>> Presumably this came up because you actually ran into a function that
>> returns an optional second value, and you needed to assign something other
>> than NIL in the M-V-BIND.  Would you care to tell us what this function is
>> and why NIL is not an appropriate default value?
>No, I was just trying to follow what the original poster (seemed to)
>want.  There doesn't seem to be any purpose to being able to default
>in MVB otherwise.

Sorry, when you said "I want", I assumed you *were* the original poster,
and didn't bother to scan back to verify this.

Like I said earlier, I think that most operators that have optional return
values do so because the extra values would be meaningless in the context
of the earlier values.  For instance (ignore-errors
<body-that-returns-1-val>) doesn't return a second value (the error object)
when the body doesn't signal an error, since there's no error object to
return (however, this is probably a bad example, since IGNORE-ERRORS has to
be prepared to return as many values as its body does when there's no
error -- this one-value situation is just a special case).

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.



Sun, 30 Nov 2003 03:11:13 GMT  
 
 [ 24 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Final SRFI 8: RECEIVE: Binding to multiple values

2. SRFI 8: RECEIVE: Binding to multiple values

3. Defining multiple-values binding forms

4. multiple-value binding let and let*

5. binding multiple values (Ex: Re: some small proposed changes to standard)

6. lambda list in MULTIPLE-VALUE-BIND?

7. with-slots and multiple-value-bind

8. Common Lisp summary and multiple value binding

9. let vs. multiple-value-bind

10. multiple-value-bind

11. (let (flet (multiple-value-bind (etc.))))

12. Question: Multiple values?

 

 
Powered by phpBB® Forum Software