[chuck-users] sporking member function and static

Spencer Salazar ssalazar at princeton.edu
Mon Jun 19 21:30:04 EDT 2006


Atte,
I know that sporking member functions is on one of the todo lists, so  
its safe to assume that that functionality isnt working completely at  
the moment.  There are plenty of good reasons to want to spork a non- 
static member function though, so it should be on its way.  One quick  
hack to simulate sporking a member function could work like this:

class Test
{
	void sporkee()
	{
		//do something
	}
}

fun void spork_helper( Test t )
{
	t.sporkee();
}

Test u;

spork ~ spork_helper( u );

"static" originated in C, but it is used in ChucK the same way that C+ 
+ and Java uses it.  Under the hood, whats happening is that there is  
a single address somewhere that holds the value of a static member  
variable.  Each time you call Class.static_data or  
Instance.static_data, its referencing the same underlying memory for  
a given class and variable name.  The name makes more sense in the  
context of C++, because compilers would statically allocate the  
memory used to hold static variables when you compiled the program.   
In ChucK, runtime and compile time are basically the same, so this  
distinction isnt as significant, but the name has stuck around.

my non-formal definition of static: the data represented by a static  
variable is the same for every instance of a class at a given point  
in time.  Changes to this data in one instance are immediately  
reflected in other classes.  Static functions are basically just like  
regular, non-class functions, except that they exist within the  
namespace of a class.

"const" (constant) is the keyword used to specify immutable data in  
Java and C++.  I dont think its currently used in ChucK, but its on  
the reserved word list, so I imagine that it will be implemented at  
some point.  Often times it is desired for static data to be constant  
also, but not always.

spencer

On Jun 19, 2006, at 7:26 PM, Atte André Jensen wrote:

> Hi
>
> I played a bit with my problem of sporking a member function and  
> found that the attached example (static_member.ck) actually *do* work.
>
> However (as the comment in the code suggest) if the function sporkee 
> () is *not* declared as a static function, I get:
> [chuck](VM): NullPointerException: shred[id=2:spork~exp], PC=[3]
>
> Could soneone provide a meaningfull explanation, why that is? And  
> is the exception (when "static" is removed) to be considered a bug  
> in chuck?
>
> Finally, what *exactly* does "static" mean? The documentation  
> states that static data and functions "are shared by all instances  
> of that class". Ok, I understand that. But apparently "static"  
> doesn't mean "cannot be changed" (which is what the word implies),  
> mostly applicable to data, as the other example (static.ck) shows.  
> So, why was the word "static" chosen, shouldn't it have been  
> "global" or "shared" or something like that? Or is there a  
> historical/traditional reason behind the word "static" that I'm not  
> aware of? Whatever the reason, I think this (that "static" doesn't  
> mean "unchangeable" and when + why it's neccessary to declare  
> things, esp. functions "static", like in the case of  
> "spork_member.ck") needs mention in the manual, since I might not  
> be the only one thrown off by this...
>
> Thanks in advance for any response.
>
> -- 
> peace, love & harmony
> Atte
>
> http://www.atte.dk      | quartet:      http://www.anagrammer.dk
> http://www.atte.dk/gps  | compositions: http://www.atte.dk/ 
> compositions
> class Test
> {
> 	fun static void sporkee()
> 	{
> 		// must be static, otherwise gives:
> 		// [chuck](VM): NullPointerException: shred[id=2:spork~exp], PC=[3]
> 		while(true)
> 		{
> 			<<<"i'm sporkee">>>;
> 			1::second => now;
> 		}
> 	}
>
> 	fun void sporker()
> 	{
> 		spork ~ sporkee();
> 	}
> }
>
> Test test;
>
> test.sporker();
>
> // keep alive
> while(true)
> {
> 	1::second => now;
> }
> class Test
> {
> 	0 => static int x;
> 	0 => int y;
> 	
> 	fun void tell_x()
> 	{
> 		<<<x>>>;
> 	}
>
> 	fun void tell_y()
> 	{
> 		<<<y>>>;
> 	}
>
> }
>
> Test test;
>
> test.tell_x();
> test.tell_y();
>
> 1 => test.x;
> 2 => test.y;
>
> test.tell_x();
> test.tell_y();
> _______________________________________________
> chuck-users mailing list
> chuck-users at lists.cs.princeton.edu
> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users



More information about the chuck-users mailing list