POV-Ray : Newsgroups : povray.off-topic : Monads in C# : Re: Monads in C# Server Time
29 Jul 2024 22:23:51 EDT (-0400)
  Re: Monads in C#  
From: Warp
Date: 2 Jul 2011 16:52:57
Message: <4e0f8529@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> OK. In C#, the generic class "Nullable" takes a type argument.

> Declaring X to be of type Nullable<T> means that X has the methods
>    X = (T) Y;  // This lets you turn a T into a Nullable<T> in other words
>    X.HasValue is a boolean that returns true.
>    X = null;   // This makes X.HasValue return false
>    X.Value returns a value of type T if and only if X.HasValue is true.

  What does X.Value return if X.HasValue is false?

> (You could probably do the same thing with multiple inheritance in C++, but 
> C# doesn't really have that.)

  I can think of two possible ways. Either multiple inheritance, like:

    class NullableInteger: public Integer, public Nullable
    {};

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; }
    };

  The latter could be used for example like:

    Nullable<int> value = 5;

> > I can't comprehend what this has to do with I/O

> [...]

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

-- 
                                                          - Warp


Post a reply to this message

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