Jump to page: 1 2 3
Thread overview
several questions
Feb 15, 2008
Saaa
Feb 15, 2008
Simen Kjaeraas
Feb 15, 2008
Saaa
Feb 15, 2008
Simen Kjaeraas
Feb 16, 2008
bearophile
Feb 16, 2008
Saaa
Feb 17, 2008
bearophile
Feb 17, 2008
Saaa
Feb 17, 2008
Simen Kjaeraas
Feb 16, 2008
Saaa
Feb 17, 2008
Christopher Wright
Feb 17, 2008
Saaa
Feb 17, 2008
Christopher Wright
Feb 17, 2008
Saaa
Feb 15, 2008
downs
Feb 15, 2008
torhu
Feb 16, 2008
Saaa
Feb 17, 2008
Sergey Gromov
Feb 17, 2008
Saaa
Feb 17, 2008
Bill Baxter
Feb 17, 2008
Saaa
Feb 16, 2008
Derek Parnell
Feb 17, 2008
torhu
Feb 17, 2008
Derek Parnell
February 15, 2008
From the D1 documentation:
For dynamic array and object parameters, which are passed by reference,
in/out/ref apply only to the reference and not the contents.

What exactly (internally memory wise) does 'passing' mean. Is it like
copying?
And does this mean that static arrays are not passed by reference and should
I use:
void func(ref array.ptr)
,because otherwise the whole array is passed (copied) ? (which sound slow:)

Another question :)

What is the advantage of making a function/variable static?
It makes it nonvirtual, right. Why is this good?
When should I make something static or final?

A private function/var can't used by anything in a 'lower' scope, right?

Well thats it for now =D


February 15, 2008
On Fri, 15 Feb 2008 20:53:21 +0100, Saaa <empty@needmail.com> wrote:

> From the D1 documentation:
> For dynamic array and object parameters, which are passed by reference,
> in/out/ref apply only to the reference and not the contents.
>
> What exactly (internally memory wise) does 'passing' mean. Is it like
> copying?

It means that when used as a parameter to a function, a reference type will just send (copy, if you will) a pointer to itself to the function, while a value type will copy all its data. 'Least I think that should be fairly correct.

> And does this mean that static arrays are not passed by reference and should
> I use:
> void func(ref array.ptr)
> ,because otherwise the whole array is passed (copied) ? (which sound slow:)

I seriously doubt ut does. Not having checked, I won't say that with 100% certainty, though.

> Another question :)
>
> What is the advantage of making a function/variable static?
> It makes it nonvirtual, right. Why is this good?
> When should I make something static or final?

A static function or variable becomes a member of the class/struct definition, instead of of the instance. Say you have

class Foo
{
	static int bar_static;	// Shared by all instances of Foo, so if one of them changes the value, it's changed for all.
	int bar;		// New int specific to each instance. Change this value, and no other instance will notice.
	static void doStuff(){}	// Function with no 'this' pointer. Can be called using Foo.doStuff(), with no instance of Foo anywhere in your program. Can change only static members of Foo.
	void doStuff(){}	// Function with 'this' pointer. Needs an instance on which to work. Can change static members of Foo, as well as normal members.
}

> A private function/var can't used by anything in a 'lower' scope, right?

Private in D means 'not accessible outside this module.' So this is perfectly legal:

class Foo
{
	private int foo;
}

class Bar : Foo
{
	void bar()
	{
		foo = 3;
	}
}

void doStuff()
{
	Foo f = new Foo();
	f.foo = 4;
}

You can however not change foo from inside a different source file (module).

> Well thats it for now =D
February 15, 2008
Thanks!
Still bit uncertain about the static array though :)
But I think I understand static now, although I never use 'this' making it a
bit more difficult to understand :)
If I say something like 'static int i=0;' within the main I can't use i for
anything else in the whole program afterwards.
It makes variables into global variables :)
What exactly is 'having a this reference' ?
And what does final mean. (the attributes page doesn't say anything about it
:/ )
Private is easy to understand, thanks.

> From the D1 documentation:
> For dynamic array and object parameters, which are passed by reference,
> in/out/ref apply only to the reference and not the contents.
>
> What exactly (internally memory wise) does 'passing' mean. Is it like
> copying?

It means that when used as a parameter to a function, a reference type will just send (copy, if you will) a pointer to itself to the function, while a value type will copy all its data. 'Least I think that should be fairly correct.

> And does this mean that static arrays are not passed by reference and
> should
> I use:
> void func(ref array.ptr)
> ,because otherwise the whole array is passed (copied) ? (which sound
> slow:)

I seriously doubt ut does. Not having checked, I won't say that with 100% certainty, though.

> Another question :)
>
> What is the advantage of making a function/variable static?
> It makes it nonvirtual, right. Why is this good?
> When should I make something static or final?

A static function or variable becomes a member of the class/struct definition, instead of of the instance. Say you have

class Foo
{
static int bar_static; // Shared by all instances of Foo, so if one of
them changes the value, it's changed for all.
int bar; // New int specific to each instance. Change this value, and no
other instance will notice.
static void doStuff(){} // Function with no 'this' pointer. Can be called
using Foo.doStuff(), with no instance of Foo anywhere in your program. Can
change only static members of Foo.
void doStuff(){} // Function with 'this' pointer. Needs an instance on
which to work. Can change static members of Foo, as well as normal members.
}

> A private function/var can't used by anything in a 'lower' scope, right?

Private in D means 'not accessible outside this module.' So this is perfectly legal:

class Foo
{
private int foo;
}

class Bar : Foo
{
void bar()
{
foo = 3;
}
}

void doStuff()
{
Foo f = new Foo();
f.foo = 4;
}

You can however not change foo from inside a different source file
(module).

> Well thats it for now =D


February 15, 2008
On Fri, 15 Feb 2008 22:09:10 +0100, Saaa <empty@needmail.com> wrote:

> Thanks!
> Still bit uncertain about the static array though :)
> But I think I understand static now, although I never use 'this' making it a
> bit more difficult to understand :)
> If I say something like 'static int i=0;' within the main I can't use i for
> anything else in the whole program afterwards.

A static variable inside a function (which I did not cover in my last post), will keep its value after the function has exited, and even when it's being called again.
This function:

void sillyFunc()
{
	static timesRun;
	timesRun++:
	writefln(timesRun);
}

will print the numer of times it's been run, for instance.

> It makes variables into global variables :)
> What exactly is 'having a this reference' ?

When you instantiate a struct or class, some piece of memory is allocated for that instantiation. When you run a non-static member function, the 'this' pointer points to this data, basically saying 'here I am, and my (again, non-static) member variables are offset from here.'

Example:

class Foo
{
	int bar;
	void baz()
	{
		bar++;		// equivalent of this.bar++
		writefln(bar);	
	}
}

void main()
{
	Foo f = new Foo();	// f now points to a memory location that will be used
	f.baz();		// as 'this' pointer inside the function bar executed here.
}

> And what does final mean. (the attributes page doesn't say anything about it

Final means 'this function cannot be overrided.' Subclasses of whatever class has a final function will not be able to implement their own versions of the final function.

class Foo
{
	final void doStuff()
	{
		writefln("Foo.doStuff();");
	}
}

class Bar : Foo
{
	void doStuff()		// here the compiler will complain "cannot override final function Foo.doStuff
	{
		writefln("Bar.doStuff();");
	}
}

> :/ )
> Private is easy to understand, thanks.
>
February 15, 2008
Saaa wrote:
> From the D1 documentation:
> For dynamic array and object parameters, which are passed by reference,
> in/out/ref apply only to the reference and not the contents.
> 
> What exactly (internally memory wise) does 'passing' mean. Is it like copying?


int x = 4;
// the value 4, assigned to x, is now outside of test.

test(x); // here I pass the value 4, in the form of x, into test.

int test ( int a )
{
 // 4, in the form of a, has been passed into test.

> And does this mean that static arrays are not passed by reference and should
> I use:
> void func(ref array.ptr)
> ,because otherwise the whole array is passed (copied) ? (which sound slow:)
> 

Complete misunderstanding.
An array is basically this with syntax candy:

struct array (T)
{
	size_t length;
	T* ptr;
}

Now, that paragraph from the documentation basically means, that if you pass an array into a function, you really just pass its length and ptr.
So all these keywords, ref, out, in .. apply to the ptr and length, not the array's contents.

Basically, an array is a reference to the data, with additional length information. That's what's meant by "dynamic array [...] parameters [...] are passed by reference".

> Another question :)
> 
> What is the advantage of making a function/variable static? It makes it nonvirtual, right. Why is this good?
Because it can be inlined.

> When should I make something static or final?
> 
When you don't want it to be virtual :)

 --downs
February 15, 2008
Saaa wrote:
> And does this mean that static arrays are not passed by reference and should I use:
> void func(ref array.ptr)
> ,because otherwise the whole array is passed (copied) ? (which sound slow:)

Static arrays as passed as the address of the first element.  So yes, that's 'by reference'.


These two are implemented in the exact same way:

void func(int[3] array);
void func(int* ptr_to_some_ints);

// used like:
int[3] array = [1, 2, 3];
func(array);
func(array.ptr);


The important difference is that inside the second function, the compiler does not care about how many elements 'array' contains, and you don't get automatic array bounds checking.
February 16, 2008
Simen Kjaeraas:
> A static variable inside a function (which I did not cover in my last post), will keep its value after the function has exited, and even when it's being called again.

It's a global variable that can be accessed/used only inside the namespace of that function.

Bye,
bearophile
February 16, 2008
What exactly is the namespace of a function?

>> A static variable inside a function (which I did not cover in my last post), will keep its value after the function has exited, and even when it's being called again.
>
> It's a global variable that can be accessed/used only inside the namespace of that function.
>
> Bye,
> bearophile


February 16, 2008
Thanks again,

I don't think I ever override a function, should I make them final then, for optimizing purposes, or is final only for classes?



> Thanks!
> Still bit uncertain about the static array though :)
> But I think I understand static now, although I never use 'this' making
> it a
> bit more difficult to understand :)
> If I say something like 'static int i=0;' within the main I can't use i
> for
> anything else in the whole program afterwards.

A static variable inside a function (which I did not cover in my last
post), will keep its value after the function has exited, and even when
it's being called again.
This function:

void sillyFunc()
{
static timesRun;
timesRun++:
writefln(timesRun);
}

will print the numer of times it's been run, for instance.

> It makes variables into global variables :)
> What exactly is 'having a this reference' ?

When you instantiate a struct or class, some piece of memory is allocated for that instantiation. When you run a non-static member function, the 'this' pointer points to this data, basically saying 'here I am, and my (again, non-static) member variables are offset from here.'

Example:

class Foo
{
int bar;
void baz()
{
bar++; // equivalent of this.bar++
writefln(bar);
}
}

void main()
{
Foo f = new Foo(); // f now points to a memory location that will be used
f.baz(); // as 'this' pointer inside the function bar executed here.
}

> And what does final mean. (the attributes page doesn't say anything  about it

Final means 'this function cannot be overrided.' Subclasses of whatever class has a final function will not be able to implement their own versions of the final function.

class Foo
{
final void doStuff()
{
writefln("Foo.doStuff();");
}
}

class Bar : Foo
{
void doStuff() // here the compiler will complain "cannot override final
function Foo.doStuff
{
writefln("Bar.doStuff();");
}
}

> :/ )
> Private is easy to understand, thanks.
> 


February 16, 2008
From what I understand now from all the replies, it would be better to change the sentence to:

For (dynamic) array and object parameters, which are passed by reference, in/out/ref apply only to the reference and not the contents.


One last question:

How about passing a huge structure. Does this need something like a pointer
to the struct?
I mean, is the passing done optimal here:

struct S{
int n1=1;
int n2=2;
..
int n999=999;
}

void func(S s){
..
}

void main (){
S t;
func(t);
..
}


« First   ‹ Prev
1 2 3