|
From: Luigi B. <lui...@gm...> - 2009-09-28 15:09:29
|
On Sat, 2009-09-26 at 17:35 +0200, Dima wrote:
> I still think we need a little wrapper instead of the pure optional
> class.
> It is not convenient to dereference the optional object in the code
> [...]
> A QuantLib rewriting example for the Instrument class:
>
> // Optional version
> //---------------------------------------------------------------------------
> public:
> inline Instrument::Instrument(){} // no initialization of member
> needed
>
> private:
> // make NPV_ an Optional variable
> Optional<Real> NPV_
>
> inline Real Instrument::NPV() const {
> calculate();
> QL_REQUIRE(NPV_.isInitialized(), "NPV not provided");
> return NPV_;
> }
> //---------------------------------------------------------------------------
>
>
> Of course, the Optional class has to be polished. Thoughts?
I don't know, I'm not fond of automatic conversions (as you might see
from the several "explicit" keywords we added to constructors...)
It's true that this means rewriting some code (or possibly a lot of it)
but I think it would be safer. And once we rule out explicit conversion,
the code is not that verbose. With boost::optional, the excerpt above
would be written as:
//---------------------------------------------------------------------------
public:
Instrument::Instrument(){} // no initialization of member needed
private:
// make NPV_ a boost::optional variable
boost::optional<Real> NPV_;
Real Instrument::NPV() const {
calculate();
QL_REQUIRE(NPV_, "NPV not provided");
return *NPV_;
}
//---------------------------------------------------------------------------
Luigi
--
The First Rule of Optimization: Don't do it.
The Second Rule of Optimization (For experts only): Don't do it yet.
-- Michael Jackson
|