www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Function Declaration - Test.d

reply Trevor Parscal <Trevor_member pathlink.com> writes:
I want to declare a function, and later, specify the contents of that function.

Maybe i missed it in the documenation, but I have been looking for quite some
time. I am a C++ programmer who recently converted to D, so this seems like
something I should be able to do.

////////////////////////////////////

int foo(int i);

int main()
{
retrun foo(5);
}

int foo(int i)
{
return i * 2;
}

////////////////////////////////////

The problem is that D won't let me declare the function, and later spectify the
actual contents of the function.

I need to be able to do this, mostly specifying the contents of the function in
another module...

Thanks in advance for your help.

I attached a real example.

Trevor Parscal
www.trevorparscal.com
trevorparscal hotmail.com
May 16 2005
next sibling parent John Demme <me teqdruid.com> writes:
Just delete the declaration.  D supports forward references, making
stuff like what you're doing unnecessary.  In addition, if you put the
function in another module, you simply import the module, no need to put
declarations in each module that wants to use the function.

John Demme

On Tue, 2005-05-17 at 04:45 +0000, Trevor Parscal wrote:
 I want to declare a function, and later, specify the contents of that function.
 
 Maybe i missed it in the documenation, but I have been looking for quite some
 time. I am a C++ programmer who recently converted to D, so this seems like
 something I should be able to do.
 
 ////////////////////////////////////
 
 int foo(int i);
 
 int main()
 {
 retrun foo(5);
 }
 
 int foo(int i)
 {
 return i * 2;
 }
 
 ////////////////////////////////////
 
 The problem is that D won't let me declare the function, and later spectify the
 actual contents of the function.
 
 I need to be able to do this, mostly specifying the contents of the function in
 another module...
 
 Thanks in advance for your help.
 
 I attached a real example.
 
 Trevor Parscal
 www.trevorparscal.com
 trevorparscal hotmail.com
 begin 0644 Test.d
 M:6UP;W)T('-T9"YC+G=I;F1O=W,N=VEN9&]W<SL-"FEM<&]R="!S=&0N<W1R
 M:6YG.PT*#0HO+R!&=6YC=&EO;G,-"F5X=&5R;B`H0RD =F]I9"!G8U]I;FET
 M*"D[#0IE>'1E<FX *$,I('9O:60 9V-?=&5R;2 I.PT*97AT97)N("A#*2!V
 M;VED(%]M:6YI=" I.PT*97AT97)N("A#*2!V;VED(%]M;V1U;&5#=&]R*"D[
 M#0IE>'1E<FX *$,I('9O:60 7VUO9'5L955N:71497-T<R I.PT*#0II;G0 
 M36%I;BAI;F]U="!I;G0 7VDI.PT*#0IE>'1E<FXH5VEN9&]W<RD :6YT(%=I
 M;DUA:6XH2$E.4U1!3D-%(%]I;G-T86YC92P 2$E.4U1!3D-%(%]P<F5V:6YS
 M=&%N8V4L($Q04U12(%]C;61L:6YE+"!I;G0 7V-M9'-H;W<I#0I[#0H ("` 
 M:6YT(')E<W5L=#L-"B` ("!G8U]I;FET*"D[#0H ("` 7VUI;FET*"D[#0H 
 M("` =')Y#0H ("` >PT*"0E?;6]D=6QE0W1O<B I.PT*"0E?;6]D=6QE56YI
 M=%1E<W1S*"D[#0H)"0T*"0EI;G0 :2`](#4[#0H)"4UE<W-A9V5";WA!*&YU
 M;&PL(&9O<FUA="A-86EN*&DI*2P (D5R<F]R(BP 34)?3TL ?"!-0E])0T].
 M15A#3$%-051)3TXI.PT*"0ER971U<FX ,#L-"B` ("!]#0H ("` 8V%T8V H
 M3V)J96-T(%]O*0T*("` ('L-" D)365S<V%G94)O>$$H;G5L;"P 8V%S="AC
 M:&%R("HI7V\N=&]3=')I;F<H*2P (D5R<F]R(BP 34)?3TL ?"!-0E])0T].
 M15A#3$%-051)3TXI.PT*"0ER97-U;'0 /2`P.PT*("` ('T-"B` ("!G8U]T
 M97)M*"D[#0H ("` <F5T=7)N(')E<W5L=#L-"GT-" T*:6YT($UA:6XH:6YO
 B=70 :6YT(%]I*0T*>PT*"7)E='5R;B!?:2`K(#4[#0I].VYT
 `
 end

May 16 2005
prev sibling next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Tue, 17 May 2005 04:45:49 +0000 (UTC), Trevor Parscal wrote:

 I want to declare a function, and later, specify the contents of that function.

Why? This isn't needed in D.
 Maybe i missed it in the documenation, but I have been looking for quite some
 time. I am a C++ programmer who recently converted to D, so this seems like
 something I should be able to do.
 
 ////////////////////////////////////
 
 int foo(int i);

The above line is redundant. D supports forward referencing.
 int main()
 {
 retrun foo(5);
 }
 
 int foo(int i)
 {
 return i * 2;
 }
 
 ////////////////////////////////////
 
 The problem is that D won't let me declare the function, and later spectify the
 actual contents of the function.

That right.
 I need to be able to do this, mostly specifying the contents of the function in
 another module...

No, you don't need to. Just use the import statement if its in another file otherwise you don't need to pre-declare it. -- Derek Melbourne, Australia 17/05/2005 3:27:51 PM
May 16 2005
parent Trevor Parscal <Trevor_member pathlink.com> writes:
In article <1hz4l06kira98.3o0cvll7zs0q$.dlg 40tude.net>, Derek Parnell says...
On Tue, 17 May 2005 04:45:49 +0000 (UTC), Trevor Parscal wrote:

 I want to declare a function, and later, specify the contents of that function.

Why? This isn't needed in D.
 Maybe i missed it in the documenation, but I have been looking for quite some
 time. I am a C++ programmer who recently converted to D, so this seems like
 something I should be able to do.
 
 ////////////////////////////////////
 
 int foo(int i);

The above line is redundant. D supports forward referencing.
 int main()
 {
 retrun foo(5);
 }
 
 int foo(int i)
 {
 return i * 2;
 }
 
 ////////////////////////////////////
 
 The problem is that D won't let me declare the function, and later spectify the
 actual contents of the function.

That right.
 I need to be able to do this, mostly specifying the contents of the function in
 another module...

No, you don't need to. Just use the import statement if its in another file otherwise you don't need to pre-declare it. -- Derek Melbourne, Australia 17/05/2005 3:27:51 PM

OK, I feel dumb, but also thankful for D's forward refrencing capabilities. Knowing C++ so well really screws me up sometimes.. Thanks to both replies. Trevor Parscal www.trevorparscal.com trevorparscal hotmail.com
May 16 2005
prev sibling parent reply "G.Vidal" <gyvidal wanadoo.fr> writes:
About that...

What if we want to distribute a library without the source code?

In C/C++ we'd have given the headers but in D...
May 18 2005
parent reply Derek Parnell <derek psych.ward> writes:
On Wed, 18 May 2005 12:48:23 +0200, G.Vidal wrote:

 About that...
 
 What if we want to distribute a library without the source code?
 
 In C/C++ we'd have given the headers but in D...

... you are also given the 'headers'. [realmod.d] module realmod; class Foo { private anInt; this(){ anInt =2; } } void Func(dchar[] a, real b) { a[0] = cast(int)b; } -------------- [headmod.d] module realmod; class Foo { this(); } void Func(dchar[] a, real b); ---------- You compile 'realmod.d' and archive 'realmod.obj' into 'reallib.lib'. You distribute 'reallib.lib' and 'headmod.d'. -- Derek Parnell Melbourne, Australia 18/05/2005 8:57:00 PM
May 18 2005
parent reply John Reimer <brk_6502 yahoo.com> writes:
Derek Parnell wrote:
 On Wed, 18 May 2005 12:48:23 +0200, G.Vidal wrote:
 
 
About that...

What if we want to distribute a library without the source code?

In C/C++ we'd have given the headers but in D...

... you are also given the 'headers'. [realmod.d] module realmod; class Foo { private anInt; this(){ anInt =2; } } void Func(dchar[] a, real b) { a[0] = cast(int)b; } -------------- [headmod.d] module realmod; class Foo { this(); } void Func(dchar[] a, real b); ---------- You compile 'realmod.d' and archive 'realmod.obj' into 'reallib.lib'. You distribute 'reallib.lib' and 'headmod.d'.

If I recall correctly, technically it's feasible for d to have a tool that extracts the required symbol information from the object file (or library), thus removing the necessity of headers. I think Walter mentioned this the last time the topic came up to counter my bemoaning the fact that headers were still necessary. He said it just wasn't implemented yet. I for one would love to see that implemented! It would sure keep the source file and import paths down to a minimum. Ooooo... just think, Derek, another nifty feature for build! ;-) -JJR
May 18 2005
parent reply Derek Parnell <derek psych.ward> writes:
On Wed, 18 May 2005 04:36:57 -0700, John Reimer wrote:

 Derek Parnell wrote:
 On Wed, 18 May 2005 12:48:23 +0200, G.Vidal wrote:
 
 
About that...

What if we want to distribute a library without the source code?

In C/C++ we'd have given the headers but in D...

... you are also given the 'headers'. [realmod.d] module realmod; class Foo { private anInt; this(){ anInt =2; } } void Func(dchar[] a, real b) { a[0] = cast(int)b; } -------------- [headmod.d] module realmod; class Foo { this(); } void Func(dchar[] a, real b); ---------- You compile 'realmod.d' and archive 'realmod.obj' into 'reallib.lib'. You distribute 'reallib.lib' and 'headmod.d'.

If I recall correctly, technically it's feasible for d to have a tool that extracts the required symbol information from the object file (or library), thus removing the necessity of headers. I think Walter mentioned this the last time the topic came up to counter my bemoaning the fact that headers were still necessary. He said it just wasn't implemented yet. I for one would love to see that implemented! It would sure keep the source file and import paths down to a minimum. Ooooo... just think, Derek, another nifty feature for build! ;-) -JJR

Its already on the TODO list ;-) -- Derek Parnell Melbourne, Australia 19/05/2005 1:56:56 AM
May 18 2005
parent reply "Charlie" <charles jwavro.com> writes:
 Its already on the TODO list ;-)

Cool! If you want to extract the symbols from the object file burton radon's digc does this with omflistexport's ( os something similar ) , worth checking out :). Charlie "Derek Parnell" <derek psych.ward> wrote in message news:1n55thixdis1t.1ne0krr04bai9.dlg 40tude.net...
 On Wed, 18 May 2005 04:36:57 -0700, John Reimer wrote:

 Derek Parnell wrote:
 On Wed, 18 May 2005 12:48:23 +0200, G.Vidal wrote:


About that...

What if we want to distribute a library without the source code?

In C/C++ we'd have given the headers but in D...

... you are also given the 'headers'. [realmod.d] module realmod; class Foo { private anInt; this(){ anInt =2; } } void Func(dchar[] a, real b) { a[0] = cast(int)b; } -------------- [headmod.d] module realmod; class Foo { this(); } void Func(dchar[] a, real b); ---------- You compile 'realmod.d' and archive 'realmod.obj' into 'reallib.lib'. You distribute 'reallib.lib' and 'headmod.d'.

If I recall correctly, technically it's feasible for d to have a tool that extracts the required symbol information from the object file (or library), thus removing the necessity of headers. I think Walter mentioned this the last time the topic came up to counter my bemoaning the fact that headers were still necessary. He said it just wasn't implemented yet. I for one would love to see that implemented! It would sure keep the source file and import paths down to a minimum. Ooooo... just think, Derek, another nifty feature for build! ;-) -JJR

Its already on the TODO list ;-) -- Derek Parnell Melbourne, Australia 19/05/2005 1:56:56 AM

May 18 2005
parent John Reimer <brk_6502 yahoo.com> writes:
Charlie wrote:
Its already on the TODO list ;-)

Cool! If you want to extract the symbols from the object file burton radon's digc does this with omflistexport's ( os something similar ) , worth checking out :). Charlie

Ohhh... I didn't see that. That's interesting. I'll have to check it out. Last I heard, Burton was also working on a new, improved digc. I wonder how that is coming along. Can't get enough of these great tools. -JJR
May 18 2005