Re: Return or Not?
From: Alwyn (dt015a1979_at_mac.com.invalid)
Date: 11/19/04
- Next message: Jonathan Mcdougall: "Re: array question"
- Previous message: Watson Davis: "Re: array question"
- In reply to: Arthur J. O'Dwyer: "Re: Return or Not?"
- Next in thread: B. v Ingen Schenau: "Re: Return or Not?"
- Reply: B. v Ingen Schenau: "Re: Return or Not?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 19 Nov 2004 07:27:30 +0000
In article
<Pine.LNX.4.60-041.0411190037200.27018@unix44.andrew.cmu.edu>, Arthur
J. O'Dwyer <ajo@nospam.andrew.cmu.edu> wrote:
>
> You're complaining ( ;-) ) that when you try to write a function that
> both returns an error code and returns an integer value, you can't do it
> unless you return a pair of values somewhere. Well, duh. Several of
> the most obvious solutions, in order of elegance (IMVHO), are:
I suppose these situations would be best served by a 'Maybe' data type
as in Haskell:
data Maybe a = Nothing | Just a
partial :: Int -> Maybe Int
which returns 'Just x' if it succeeds and 'Nothing' if it fails.
I suppose it could be approximated, though somewhat clunkily, in C++:
emun Perhaps {Nothing, Just};
template <class T>
struct Maybe
{
Perhaps c;
T v;
};
Maybe<int> partial(int n);
then if 'c' in the returned structure has a value of 'Nothing', you
ignore 'v' but can use it if 'c' is 'Just'.
But I still think there's a strong case for using an
'std::domain_error' exception here. Isn't this what it's for, after
all?
Alwyn
- Next message: Jonathan Mcdougall: "Re: array question"
- Previous message: Watson Davis: "Re: array question"
- In reply to: Arthur J. O'Dwyer: "Re: Return or Not?"
- Next in thread: B. v Ingen Schenau: "Re: Return or Not?"
- Reply: B. v Ingen Schenau: "Re: Return or Not?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|