Re: The Null<> Class

Posted by Luigi Ballabio on
URL: http://quantlib.414.s1.nabble.com/The-Null-Class-tp13063p13070.html

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



------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev