www.digitalmars.com         C & C++   DMDScript  

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

reply 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)

?

or what about...

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
next sibling parent reply Chris Sauls <ibisbasenji gmail.com> writes:
clayasaurus wrote:
 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)

 or what about...
 
 version( ONE || !TWO)

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

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.

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
parent 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.
 or what about...

 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
prev sibling next sibling parent reply 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 { }
 
 or what about...
 
 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
next sibling parent reply 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 { }
 
 or what about...
 
 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
parent 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 { }
or what about...

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
prev sibling parent 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?
 
 
or what about...

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
prev sibling next sibling parent reply 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
parent reply "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
parent 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
prev sibling next sibling parent reply 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
parent reply 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
next sibling parent 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
prev sibling parent reply 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
next sibling parent 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
prev sibling parent reply 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
parent 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?

Yet another argument in favor of a (standard) reflection mechanism for D. -- Chris Sauls
Oct 30 2005
prev sibling parent reply 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
next sibling parent reply "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
parent reply 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

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
parent "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()' ?

Regan

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
prev sibling parent "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