January 12, 2007
Frits van Bommel wrote:
> Sebastian Biallas wrote:
>> BtW: What exacly happens on:
>>
>> a = b ~ c and a ~= b
>>
>> ? Is this some build-in opCat? What are the semantics?
> 
> You can see it as a built-in opCat if you like.
> What happens behind the scenes is that a function in the runtime is called.
> The source to these functions is in dmd/src/phobos/internal/gc/gc.d if
> you really want to know exactly what they do... (_d_arraycat for ~,
> _d_arrayappend for ~= with array, _d_arrayappendc for ~= with single
> element)

Well, I found out by inspecting the disassembly, but I prefer to have spec on which I can rely on.
January 12, 2007
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.

[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
January 12, 2007
"Sebastian Biallas" <groups.5.sepp@spamgourmet.com> wrote in message news:eo6ssg$1vff$1@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?


January 12, 2007
Jarrett Billingsley wrote:
> "Sebastian Biallas" <groups.5.sepp@spamgourmet.com> wrote in message
>> 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:

[snip]

And they might be implemented as:
char[] Title()
{
	return mTitle;
}

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

In which case the code just fails in case of an error.

My point is, that hypothetical C++ solution can't fail, just because of
some odd implementation:
{
	std::string origTitle;
	msg.getTitle(origTitle);
	scope(exit) msg.SetTitle(origTitle);
	msg.SetTitle("[Sending] " + origTitle);
	Copy(msg, "Sent");
}
[I rely here on some imagenary scope keyword in C++]

By reading the C++ code it is /obvious/ that I have a copy of origTitle.

> 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.

See above :)

>> [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?

Oh, I didn't know that :)
January 12, 2007
Reply to Sebastian,

> 
> So, new question: How to I pass T[] to foo(), so that foo() isn't
> allowed to change the content of T[]?
> 

T[] abc = //somthing

foo(abc.dup);

the .dup propriety does a GC memory allocation and a memcopy of the old array for any array.


January 12, 2007
Sebastian Biallas wrote:
> Frits van Bommel wrote:
>> Sebastian Biallas wrote:
>>> BtW: What exacly happens on:
>>>
>>> a = b ~ c and a ~= b
>>>
>>> ? Is this some build-in opCat? What are the semantics?
>> You can see it as a built-in opCat if you like.
>> What happens behind the scenes is that a function in the runtime is called.
>> The source to these functions is in dmd/src/phobos/internal/gc/gc.d if
>> you really want to know exactly what they do... (_d_arraycat for ~,
>> _d_arrayappend for ~= with array, _d_arrayappendc for ~= with single
>> element)
> 
> Well, I found out by inspecting the disassembly, but I prefer to have
> spec on which I can rely on.

This isn't in the spec. And it shouldn't be in the spec. This is (compiler) implementation-dependent stuff.

All you need to know is that it works ;).
January 12, 2007
BCS wrote:
> Reply to Sebastian,
> 
>>
>> So, new question: How to I pass T[] to foo(), so that foo() isn't
>> allowed to change the content of T[]?
> 
> T[] abc = //somthing
> 
> foo(abc.dup);
> 
> the .dup propriety does a GC memory allocation and a memcopy of the old array for any array.

What if the array is really big? It seems like a waste for me, to duplicate it "just in question".
January 12, 2007
"Sebastian Biallas" <groups.5.sepp@spamgourmet.com> wrote in message news:eo6vbc$22c4$1@digitaldaemon.com...

> And they might be implemented as:
> char[] Title()
> {
> return mTitle;
> }
>
> void SetTitle(char[] title)
> {
> mTitle.length = title.length;
> mTitle[] = title;
> }
>
> In which case the code just fails in case of an error.

Then change it to:

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

    if(mTitle !is title)
        mTitle[] = title;
}

No more overlapping array copy error, and you also avoid unnecessarily copying the array if mTitle already points to it.

At least, I guess that's what error you're talking about, that's the only error I get.

> My point is, that hypothetical C++ solution can't fail, just because of
> some odd implementation:
> {
> std::string origTitle;
> msg.getTitle(origTitle);
> scope(exit) msg.SetTitle(origTitle);
> msg.SetTitle("[Sending] " + origTitle);
> Copy(msg, "Sent");
> }
> [I rely here on some imagenary scope keyword in C++]
>
> By reading the C++ code it is /obvious/ that I have a copy of origTitle.

Really?  ;)  I wouldn't have known that, but maybe that's just because I don't know much about C++.  How does "msg.getTitle(origTitle)" guarantee that I'm getting a copy of the title?

>> Because Walter's weird like that?
>
> Oh, I didn't know that :)

Oh!  Well, get used to it.  ;)


1 2
Next ›   Last »