## digitalmars.D.learn - a slew of questions about D...

• clayasaurus (14/14) Oct 28 2005 1) can you do ... int function(const value) in D? or function(int value)...
• Chris Sauls (12/25) Oct 29 2005 Should be able.
• Derek Parnell (13/18) Oct 29 2005 No. You need to do ...
• J C Calvarese (7/20) Oct 29 2005 Actually it looks like he's trying to do "! TWO", so I'd use an else:
• clayasaurus (6/39) Oct 29 2005 Yup. :-P Adding a simple ! to the version would make things much better,...
• clayasaurus (23/48) Oct 29 2005 Oh ok. Seems like a bit of a hack though. Imagine if I wanted to do
• clayasaurus (18/18) Oct 30 2005 I have another quick question...
• Jarrett Billingsley (14/17) Oct 30 2005 It's mostly not needed because of the better alignment support in D. In...
• clayasaurus (3/27) Oct 30 2005 Ah, that explains it. Thanks : )
• clayasaurus (26/26) Oct 30 2005 Another converting problem.. not sure if this one can be solved with D.
• Derek Parnell (7/27) Oct 30 2005 --
• clayasaurus (27/59) Oct 30 2005 from the function pointer docs http://www.digitalmars.com/d/type.html ,
• Derek Parnell (20/39) Oct 30 2005 Sorry, I wasn't thinking ;-)
• clayasaurus (4/47) Oct 30 2005 Ahh.. thanks. It is amazing how that got away from me. I meticulously
• clayasaurus (4/47) Oct 30 2005 I might as well ask this longshot question while I'm at it.
• Chris Sauls (4/61) Oct 30 2005 In an obvious fashion... no, not that I know of. Although it would be a...
• clayasaurus (2/2) Oct 30 2005 It also appears that the = operator in D is not overloadable. Should I
• Regan Heath (4/6) Oct 30 2005 I would call it "dup" as that is what arrays use for a deep copy.
• Bruno Medeiros (9/19) Oct 31 2005 Arrays don't have the two concepts of shallow copy or deep copy, only of...
• Walter Bright (5/7) Nov 02 2005 One of the reasons '=' is not overloadable is because of endless confusi...
clayasaurus <clayasaurus gmail.com> writes:
```1) can you do ... int function(const value) in D? or function(int value)
const?

2) can you do versioning like...

static if (version == ONE || version == TWO)

or version(!Windows)

?

version( ONE || !TWO)

3) can D do default values like c++? function(int num = 0)
and you can either call function() for function(5)

4) How would I convert the command #pragma warning( disable : 4800 ) to
D? I'm not too keen on pragmas.

Thanks

~ Clay
```
Oct 28 2005
Chris Sauls <ibisbasenji gmail.com> writes:
```clayasaurus wrote:
1) can you do ... int function(const value) in D? or function(int value)
const?

Technically (as I understand it) no.  Although it may not be /as/ neccessary.

2) can you do versioning like...

static if (version == ONE || version == TWO)

Should be able.

or version(!Windows)

I wish.

version( ONE || !TWO)

I wish again.

3) can D do default values like c++? function(int num = 0)
and you can either call function() for function(5)

Yep.  For some time now.
http://www.digitalmars.com/d/function.html

4) How would I convert the command #pragma warning( disable : 4800 ) to
D? I'm not too keen on pragmas.

In this case, I believe you'd just skip it.  There's not much reason to
ignore-flag
warnings in a compiler that ordinarily doesn't do warnings anyhow.  Although
now that we
have the -w switch maybe there should be a disable pragma...  Not sure.  I
don't use -w
all that often anyhow.

-- Chris Sauls
```
Oct 29 2005
clayasaurus <clayasaurus gmail.com> writes:
```Chris Sauls wrote:
clayasaurus wrote:

1) can you do ... int function(const value) in D? or function(int
value) const?

Technically (as I understand it) no.  Although it may not be /as/
neccessary.

2) can you do versioning like...

static if (version == ONE || version == TWO)

Should be able.

I wasn't able to do so, so I just used Derek's suggestion.

or version(!Windows)

I wish.

version( ONE || !TWO)

I wish again.

3) can D do default values like c++? function(int num = 0)
and you can either call function() for function(5)

Yep.  For some time now.
http://www.digitalmars.com/d/function.html

Cool.

4) How would I convert the command #pragma warning( disable : 4800 )
to D? I'm not too keen on pragmas.

In this case, I believe you'd just skip it.  There's not much reason to
ignore-flag warnings in a compiler that ordinarily doesn't do warnings
anyhow.  Although now that we have the -w switch maybe there should be a
disable pragma...  Not sure.  I don't use -w all that often anyhow.

Yea, i'll just ignore it I guess. It is not so important in the scheme
of things.

-- Chris Sauls

Thanks

~ Clay
```
Oct 29 2005
Derek Parnell <derek psych.ward> writes:
```On Sat, 29 Oct 2005 02:59:34 -0400, clayasaurus wrote:

or version(!Windows)

No. You need to do ...

version(Windows) else { }

version( ONE || !TWO)

No. You need to do ...

version(ONE) version = X
version(TWO) version = X
version(X) { . . . }

--
Derek Parnell
Melbourne, Australia
29/10/2005 9:49:38 PM
```
Oct 29 2005
J C Calvarese <technocrat7 gmail.com> writes:
```In article <98m793lpudd4\$.1xzi56dp4vb37\$.dlg 40tude.net>, Derek Parnell says...
On Sat, 29 Oct 2005 02:59:34 -0400, clayasaurus wrote:

or version(!Windows)

No. You need to do ...

version(Windows) else { }

version( ONE || !TWO)

No. You need to do ...

version(ONE) version = X
version(TWO) version = X
version(X) { . . . }

Actually it looks like he's trying to do "! TWO", so I'd use an else:

version(ONE) version = X;
version(TWO) {} else version = X;
version(X) { . . . }

Even uglier more verbose, but I think it'll work.

jcc7
```
Oct 29 2005
clayasaurus <clayasaurus gmail.com> writes:
```J C Calvarese wrote:
In article <98m793lpudd4\$.1xzi56dp4vb37\$.dlg 40tude.net>, Derek Parnell says...

On Sat, 29 Oct 2005 02:59:34 -0400, clayasaurus wrote:

or version(!Windows)

No. You need to do ...

version(Windows) else { }

version( ONE || !TWO)

No. You need to do ...

version(ONE) version = X
version(TWO) version = X
version(X) { . . . }

Actually it looks like he's trying to do "! TWO", so I'd use an else:

version(ONE) version = X;
version(TWO) {} else version = X;
version(X) { . . . }

Even uglier more verbose, but I think it'll work.

jcc7

Yup. :-P Adding a simple ! to the version would make things much better,
&& and || would be nice too, but I'm not sure how diffuclt these things
would be to implement. I can live with 'else' for now.

Thanks

~ Clay
```
Oct 29 2005
clayasaurus <clayasaurus gmail.com> writes:
```Derek Parnell wrote:
On Sat, 29 Oct 2005 02:59:34 -0400, clayasaurus wrote:

or version(!Windows)

No. You need to do ...

version(Windows) else { }

Oh ok. Seems like a bit of a hack though. Imagine if I wanted to do

version(!Windows)
{
}
else
{
}

Then I'm forced to do something like...
version = TRUE

version(Windows)
{
}
else version(TRUE) // version !Windows
{
}
else
{
}

Unless there is another way around?

version( ONE || !TWO)

No. You need to do ...

version(ONE) version = X
version(TWO) version = X
version(X) { . . . }

I don't mind this as much as the !version hack.

Thanks.

~ Clay
```
Oct 29 2005
clayasaurus <clayasaurus gmail.com> writes:
```I have another quick question...

does D have the c++ equivilent of #pragma pack(push,1), is it not needed
with D, or just not implemented.

In the C++ code it is implemented like...

#pragma pack(push,1)

#pragma pack(1)

struct
{
a, b, c;
}

#pragma pack(1)

struct
{
...
}

#pragma(pop, 1)

Can I get the D equivilent just by using the align attribute? Thanks.

~ Clay
```
Oct 30 2005
"Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
```"clayasaurus" <clayasaurus gmail.com> wrote in message
news:dk3cit\$klj\$1 digitaldaemon.com...
does D have the c++ equivilent of #pragma pack(push,1), is it not needed
with D, or just not implemented.
Can I get the D equivilent just by using the align attribute? Thanks.

It's mostly not needed because of the better alignment support in D.  In
C++, the pack push and pop operations are needed because normally member
alignment is controlled by a command-line switch, so the pushing and popping
is so that changing the alignment for one struct doesn't change it for all
the rest.  But in D, to change the alignment for one struct, you just use
the very obvious syntax:

align(1) struct Something
{
...
}

And the alignment will stay the same (i.e. the default, or whatever you set
them to) on all the other structs in your program.
```
Oct 30 2005
clayasaurus <clayasaurus gmail.com> writes:
```Ah, that explains it. Thanks : )

~ Clay

Jarrett Billingsley wrote:
"clayasaurus" <clayasaurus gmail.com> wrote in message
news:dk3cit\$klj\$1 digitaldaemon.com...

does D have the c++ equivilent of #pragma pack(push,1), is it not needed
with D, or just not implemented.
Can I get the D equivilent just by using the align attribute? Thanks.

It's mostly not needed because of the better alignment support in D.  In
C++, the pack push and pop operations are needed because normally member
alignment is controlled by a command-line switch, so the pushing and popping
is so that changing the alignment for one struct doesn't change it for all
the rest.  But in D, to change the alignment for one struct, you just use
the very obvious syntax:

align(1) struct Something
{
...
}

And the alignment will stay the same (i.e. the default, or whatever you set
them to) on all the other structs in your program.

```
Oct 30 2005
clayasaurus <clayasaurus gmail.com> writes:
```Another converting problem.. not sure if this one can be solved with D.

#define REGISTER_AS_REMOTE_PROCEDURE_CALL(networkObject, functionName)
(networkObject)->RegisterAsRemoteProcedureCall((#functionName),(functionName))

I'm pretty sure #functionName turns it into a char*, while functionName
is the function pointer itself.

Here's my conversion attempt

template REGISTER_AS_REMOTE_PROCEDURE_CALL(T)
{
REGISTER_AS_REMOTE_PROCEDURE_CALL(T networkObject, char* uniqueID,
void function(char *input, int numberOfBitsOfData, PlayerID sender)
functionName)
{
networkObject.RegisterAsRemoteProcedureCall(uniqueID ,
functionName); // line 480
}
}

but I get

raknet/networktypes.d(480): found 'networkObject' when expecting ')'
raknet/networktypes.d(480): no identifier for declarator
REGISTER_AS_REMOTE_PROCEDURE_CALL
raknet/networktypes.d(480): semicolon expected, not 'char'
raknet/networktypes.d(480): no identifier for declarator char*
raknet/networktypes.d(480): semicolon expected, not 'void'
raknet/networktypes.d(480): semicolon expected, not ')'
raknet/networktypes.d(480): Declaration expected, not ')'
raknet/networktypes.d(484): unrecognized declaration
```
Oct 30 2005
Derek Parnell <derek psych.ward> writes:
```On Sun, 30 Oct 2005 22:53:51 -0500, clayasaurus wrote:

Another converting problem.. not sure if this one can be solved with D.

#define REGISTER_AS_REMOTE_PROCEDURE_CALL(networkObject, functionName)
(networkObject)->RegisterAsRemoteProcedureCall((#functionName),(functionName))

I'm pretty sure #functionName turns it into a char*, while functionName
is the function pointer itself.

Here's my conversion attempt

template REGISTER_AS_REMOTE_PROCEDURE_CALL(T)
{
REGISTER_AS_REMOTE_PROCEDURE_CALL(T networkObject, char* uniqueID,
void function(char *input, int numberOfBitsOfData, PlayerID sender)

,  // <<<< NEEDS A COMMA ?

functionName)
{
networkObject.RegisterAsRemoteProcedureCall(uniqueID ,
functionName); // line 480
}
}

--
Derek
(skype: derek.j.parnell)
Melbourne, Australia
31/10/2005 2:58:53 PM
```
Oct 30 2005
clayasaurus <clayasaurus gmail.com> writes:
```Derek Parnell wrote:
On Sun, 30 Oct 2005 22:53:51 -0500, clayasaurus wrote:

Another converting problem.. not sure if this one can be solved with D.

#define REGISTER_AS_REMOTE_PROCEDURE_CALL(networkObject, functionName)
(networkObject)->RegisterAsRemoteProcedureCall((#functionName),(functionName))

I'm pretty sure #functionName turns it into a char*, while functionName
is the function pointer itself.

Here's my conversion attempt

template REGISTER_AS_REMOTE_PROCEDURE_CALL(T)
{
REGISTER_AS_REMOTE_PROCEDURE_CALL(T networkObject, char* uniqueID,
void function(char *input, int numberOfBitsOfData, PlayerID sender)

,  // <<<< NEEDS A COMMA ?

from the function pointer docs http://www.digitalmars.com/d/type.html ,
it uses

int function(int) fp;	// fp is pointer to a function

from there i assume

void function(char *input, int numberOfBitsOfData, PlayerID sender)
functionName;

is valid. my logic then thinks the following should work...

void somefunc(void function(char *input, int numberOfBitsOfData,
PlayerID sender) fp)
{
fp(); // call fp
}

or should i try

typedef void function(char *input, int numberOfBitsOfData, PlayerID
sender) fp;

void somefunc(fp funcName)
{
funcName();
}

but I don't understand why I would need

void somefunc(void function(char *input, int numberOfBitsOfData,
PlayerID sender), fp)
{
fp();
}

*confused*

functionName)
{
networkObject.RegisterAsRemoteProcedureCall(uniqueID ,
functionName); // line 480
}
}

```
Oct 30 2005
Derek Parnell <derek psych.ward> writes:
```On Mon, 31 Oct 2005 14:59:39 +1100, Derek Parnell wrote:

On Sun, 30 Oct 2005 22:53:51 -0500, clayasaurus wrote:

Another converting problem.. not sure if this one can be solved with D.

#define REGISTER_AS_REMOTE_PROCEDURE_CALL(networkObject, functionName)
(networkObject)->RegisterAsRemoteProcedureCall((#functionName),(functionName))

I'm pretty sure #functionName turns it into a char*, while functionName
is the function pointer itself.

Here's my conversion attempt

template REGISTER_AS_REMOTE_PROCEDURE_CALL(T)
{
REGISTER_AS_REMOTE_PROCEDURE_CALL(T networkObject, char* uniqueID,
void function(char *input, int numberOfBitsOfData, PlayerID sender)

,  // <<<< NEEDS A COMMA ?

Sorry, I wasn't thinking ;-)

The function called "REGISTER_AS_REMOTE_PROCEDURE_CALL", inside the
template of the same name, needs to specify it return type.

For example, this compiles ...

template REGISTER_AS_REMOTE_PROCEDURE_CALL(T)
{
void REGISTER_AS_REMOTE_PROCEDURE_CALL(T networkObject, char* uniqueID,
void function(char *input, int numberOfBitsOfData, PlayerID sender)
functionName)
{
networkObject.RegisterAsRemoteProcedureCall(uniqueID ,
functionName); // line 480
}
}

--
Derek
(skype: derek.j.parnell)
Melbourne, Australia
31/10/2005 3:14:18 PM
```
Oct 30 2005
clayasaurus <clayasaurus gmail.com> writes:
```Derek Parnell wrote:
On Mon, 31 Oct 2005 14:59:39 +1100, Derek Parnell wrote:

On Sun, 30 Oct 2005 22:53:51 -0500, clayasaurus wrote:

Another converting problem.. not sure if this one can be solved with D.

#define REGISTER_AS_REMOTE_PROCEDURE_CALL(networkObject, functionName)
(networkObject)->RegisterAsRemoteProcedureCall((#functionName),(functionName))

I'm pretty sure #functionName turns it into a char*, while functionName
is the function pointer itself.

Here's my conversion attempt

template REGISTER_AS_REMOTE_PROCEDURE_CALL(T)
{
REGISTER_AS_REMOTE_PROCEDURE_CALL(T networkObject, char* uniqueID,
void function(char *input, int numberOfBitsOfData, PlayerID sender)

,  // <<<< NEEDS A COMMA ?

Sorry, I wasn't thinking ;-)

No prob.

The function called "REGISTER_AS_REMOTE_PROCEDURE_CALL", inside the
template of the same name, needs to specify it return type.

For example, this compiles ...

template REGISTER_AS_REMOTE_PROCEDURE_CALL(T)
{
void REGISTER_AS_REMOTE_PROCEDURE_CALL(T networkObject, char* uniqueID,
void function(char *input, int numberOfBitsOfData, PlayerID sender)
functionName)
{
networkObject.RegisterAsRemoteProcedureCall(uniqueID ,
functionName); // line 480
}
}

Ahh.. thanks. It is amazing how that got away from me. I meticulously
plan and read docs and I forget a void *doh*
```
Oct 30 2005
clayasaurus <clayasaurus gmail.com> writes:
```Derek Parnell wrote:
On Mon, 31 Oct 2005 14:59:39 +1100, Derek Parnell wrote:

On Sun, 30 Oct 2005 22:53:51 -0500, clayasaurus wrote:

Another converting problem.. not sure if this one can be solved with D.

#define REGISTER_AS_REMOTE_PROCEDURE_CALL(networkObject, functionName)
(networkObject)->RegisterAsRemoteProcedureCall((#functionName),(functionName))

I'm pretty sure #functionName turns it into a char*, while functionName
is the function pointer itself.

Here's my conversion attempt

template REGISTER_AS_REMOTE_PROCEDURE_CALL(T)
{
REGISTER_AS_REMOTE_PROCEDURE_CALL(T networkObject, char* uniqueID,
void function(char *input, int numberOfBitsOfData, PlayerID sender)

,  // <<<< NEEDS A COMMA ?

Sorry, I wasn't thinking ;-)

The function called "REGISTER_AS_REMOTE_PROCEDURE_CALL", inside the
template of the same name, needs to specify it return type.

For example, this compiles ...

template REGISTER_AS_REMOTE_PROCEDURE_CALL(T)
{
void REGISTER_AS_REMOTE_PROCEDURE_CALL(T networkObject, char* uniqueID,
void function(char *input, int numberOfBitsOfData, PlayerID sender)
functionName)
{
networkObject.RegisterAsRemoteProcedureCall(uniqueID ,
functionName); // line 480
}
}

I might as well ask this longshot question while I'm at it.

Is it possible to convert functionName into a character string of the
actual function name it is pointing to?
```
Oct 30 2005
Chris Sauls <ibisbasenji gmail.com> writes:
```clayasaurus wrote:
Derek Parnell wrote:

On Mon, 31 Oct 2005 14:59:39 +1100, Derek Parnell wrote:

On Sun, 30 Oct 2005 22:53:51 -0500, clayasaurus wrote:

Another converting problem.. not sure if this one can be solved with D.

#define REGISTER_AS_REMOTE_PROCEDURE_CALL(networkObject,
functionName)
(networkObject)->RegisterAsRemoteProcedureCall((#functionName),(functionName))

I'm pretty sure #functionName turns it into a char*, while
functionName is the function pointer itself.

Here's my conversion attempt

template REGISTER_AS_REMOTE_PROCEDURE_CALL(T)
{
REGISTER_AS_REMOTE_PROCEDURE_CALL(T networkObject, char*
uniqueID, void function(char *input, int numberOfBitsOfData,
PlayerID sender)

,  // <<<< NEEDS A COMMA ?

Sorry, I wasn't thinking ;-)

The function called "REGISTER_AS_REMOTE_PROCEDURE_CALL", inside the
template of the same name, needs to specify it return type.

For example, this compiles ...

template REGISTER_AS_REMOTE_PROCEDURE_CALL(T)
{
void REGISTER_AS_REMOTE_PROCEDURE_CALL(T networkObject, char*
uniqueID,
void function(char *input, int numberOfBitsOfData, PlayerID sender)
functionName)
{
networkObject.RegisterAsRemoteProcedureCall(uniqueID ,
functionName); // line 480
}
}

I might as well ask this longshot question while I'm at it.

Is it possible to convert functionName into a character string of the
actual function name it is pointing to?

In an obvious fashion... no, not that I know of.  Although it would be a killer
feature.
Yet another argument in favor of a (standard) reflection mechanism for D.

-- Chris Sauls
```
Oct 30 2005
clayasaurus <clayasaurus gmail.com> writes:
```It also appears that the = operator in D is not overloadable. Should I
settle with a function called 'deepCopy()' ?
```
Oct 30 2005
"Regan Heath" <regan netwin.co.nz> writes:
```On Mon, 31 Oct 2005 00:30:17 -0500, clayasaurus <clayasaurus gmail.com>
wrote:
It also appears that the = operator in D is not overloadable. Should I
settle with a function called 'deepCopy()' ?

I would call it "dup" as that is what arrays use for a deep copy.

Regan
```
Oct 30 2005
Bruno Medeiros <daiphoenixNO SPAMlycos.com> writes:
```Regan Heath wrote:
On Mon, 31 Oct 2005 00:30:17 -0500, clayasaurus <clayasaurus gmail.com>
wrote:

It also appears that the = operator in D is not overloadable. Should
I  settle with a function called 'deepCopy()' ?

I would call it "dup" as that is what arrays use for a deep copy.

Regan

Arrays don't have the two concepts of shallow copy or deep copy, only of
simple copy/duplication, which, if compared to object cloning/copying,
would be 'equivalent' to shallow copying, not deep copying (but such
comparision should not be made in the first place).

--
Bruno Medeiros - CS/E student
"Certain aspects of D are a pathway to many abilities some consider to
be... unnatural."
```
Oct 31 2005
"Regan Heath" <regan netwin.co.nz> writes:
```On Mon, 31 Oct 2005 15:14:15 +0000, Bruno Medeiros
<daiphoenixNO SPAMlycos.com> wrote:
Regan Heath wrote:
On Mon, 31 Oct 2005 00:30:17 -0500, clayasaurus
<clayasaurus gmail.com>  wrote:

It also appears that the = operator in D is not overloadable. Should
I  settle with a function called 'deepCopy()' ?

I would call it "dup" as that is what arrays use for a deep copy.
Regan

Arrays don't have the two concepts of shallow copy or deep copy, only of
simple copy/duplication, which, if compared to object cloning/copying,
would be 'equivalent' to shallow copying, not deep copying (but such
comparision should not be made in the first place).

You're right, of course.

Regan
```
Oct 31 2005
"Walter Bright" <newshound digitalmars.com> writes:
```"clayasaurus" <clayasaurus gmail.com> wrote in message
news:dk4a31\$29c1\$1 digitaldaemon.com...
It also appears that the = operator in D is not overloadable. Should I
settle with a function called 'deepCopy()' ?

One of the reasons '=' is not overloadable is because of endless confusion
over whether it should be a deep or a shallow copy. By all means, if you're
doing a deepCopy(), name it that!
```
Nov 02 2005