POV-Ray : Newsgroups : povray.off-topic : Monads in C# : Re: Monads in C# Server Time
29 Jul 2024 22:32:16 EDT (-0400)
  Re: Monads in C#  
From: Darren New
Date: 2 Jul 2011 18:55:42
Message: <4e0fa1ee$1@news.povray.org>
On 7/2/2011 13:52, Warp wrote:
>    What does X.Value return if X.HasValue is false?

In C#, I believe it throws an exception.

>    I can think of two possible ways. Either multiple inheritance, like:
>
>      class NullableInteger: public Integer, public Nullable
>      {};

I don't think that works, because you need a way to get to the integer value 
from a nullableinteger.  Hmmm, well, maybe, now that I think about it, yes.

> or using composition with a template class, like:
>
>      template<typename T>
>      class Nullable
>      {
>          T mValue;
>          bool mHasValue;
>
>       public:
>          Nullable(): mHasValue(false) {}
>          Nullable(const T&  value): mValue(value), mHasValue(true) {}
>
>          void setNull(bool b) { mHasValue = !b; }
>
>          bool hasValue() const { return mHasValue; }
>          T&  value() { return mValue; }
>          const T&  value() const { return mValue; }
>      };

Wow. I actually understood all that. :-)

>    So it's all related to preserving the order in which I/O is performed?

While I'm certainly not the expert here ;-) I think that's correct. In order 
for I/O to happen in the right order in a purely functional language, you 
have to arrange for the result of your first I/O to go to the second I/O 
call. Otherwise, the two could go in either order. So doing I/O in the right 
order is the same mechanism as making sure the second thing in a sequence 
isn't evaluated if the first thing throws an exception, and etc.

-- 
Darren New, San Diego CA, USA (PST)
   "Coding without comments is like
    driving without turn signals."


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.