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
begin 0644 Test.d
M:6UP;W)T('-T9"YC+G=I;F1O=W,N=VEN9&]W<SL-"FEM<&]R="!S=&0N<W1R





M;DUA:6XH2$E.4U1!3D-%(%]I;G-T86YC92P 2$E.4U1!3D-%(%]P<F5V:6YS

M:6YT(')E<W5L=#L-


M;&PL(&9O<FUA="A-86EN*&DI*2P (D5R<F]R(BP 34)?3TL ?"!-0E])0T].

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



`
end
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;DUA:6XH2$E.4U1!3D-%(%]I;G-T86YC92P 2$E.4U1!3D-%(%]P<F5V:6YS

 M:6YT(')E<W5L=#L-


 M;&PL(&9O<FUA="A-86EN*&DI*2P (D5R<F]R(BP 34)?3TL ?"!-0E])0T].

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



 `
 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