Documentation of D arrays

Jarrett Billingsley kb3ctd2 at yahoo.com
Thu Jan 11 19:09:57 PST 2007


"Sebastian Biallas" <groups.5.sepp at spamgourmet.com> wrote in message 
news:eo6ssg$1vff$1 at digitaldaemon.com...
> Here is some nice example of the confusion I have with D arrays:
>
> On the really interesting page
> http://www.digitalmars.com/d/exception-safe.html
> this is shown:
>
> class Mailer
> {
>    void Send(Message msg)
>    {
> {
>     char[] origTitle = msg.Title();
>     scope(exit) msg.SetTitle(origTitle);
>     msg.SetTitle("[Sending] " ~ origTitle);
>     Copy(msg, "Sent");
> }
> scope(success) SetTitle(msg.ID(), "Sent", msg.Title);
> scope(failure) Remove(msg.ID(), "Sent");
> SmtpSend(msg); // do the least reliable part last
>    }
> }
>
> The scope(xx) things are cool, ok. The question in this example is:
>
> What exactly happens in msg.Title() and msg.SetTitle()? [1]
> *) Does msg.Title return a reference or a copy?
> *) Does msg.SetTitle copy the reference or the content?
>
> It is (for me) not obvious that the "scope(exit)" clause really works.

I think maybe you're putting too much thought into this :)

How msg.Title and msg.SetTitle are implemented, in this case, don't really 
matter.  They might do something like:

char[] Title()
{
    return mTitle.dup;
}

void SetTitle(char[] title)
{
    mTitle = title.dup;
}

In which case, the internal title is always kept secret from everything 
else, so Title returns a copy and SetTitle always copies its argument; or 
they could be implemented as:

char[] Title()
{
    return mTitle;
}

void SetTitle(char[] title)
{
    mTitle = title;
}

In which case the internal title is just a reference to a string that's set 
to it.  Modifying the string in the calling function after setting the title 
would then cause the internal title to change.

How these functions are implemented would be (1) completely up to the 
implementer of the class, and (2) for the most part, invisible to the users 
of the class.  The code example is the same no matter how these functions 
are implemented.

> [1] And another question is: Why are they names Capitalized? The
> style-guide[2] says that function names should start with a lower case
> letter.
> [2] http://www.digitalmars.com/d/dstyle.html

Because Walter's weird like that? 





More information about the Digitalmars-d mailing list