static and new

proc mymethod() static d as new ThingAMaBob ... ... end proc
Will that line only call new once?

[quote=383432:@Brian O’Brien]proc mymethod() static d as new ThingAMaBob ... ... end proc
Will that line only call new once?[/quote]

Unless the constructor raises an exception, yes.

But, as I remember, value will be equal across all instances of the class.
Is this still so?

Yes. That’s what Static is for.

It’s exactly like storing the value in a Module’s property or class’ Shared Property, just quicker and less clutter-y.

I’m very familiar with the concept of static as I am a c/c++ programmer.
But syntactically the Xojo statement seems ambiguous.

void fn(void) { static void * p = malloc(10); ... }
I believe this code would allocate another 10 bytes each time fn is called.
The pointer itself is not auto but static and there is only ever one instance of it.

static x as new thing

The above statement will only ever do one allocation.

That C++ code would not allocate every time the function is called. Both C++ and Xojo will only execute a static variable’s initializer once.

Example:

#include <stdlib.h>
#include <stdio.h>

void * Alloc(size_t size) {
	puts(__FUNCTION__);
	return malloc(size);
}

void Test() {
	static void *bar = Alloc(8);
	printf("%p\
", bar);
}

int main() {
	Test();
	Test();
	Test();
}

puts out:

Alloc
0x7f9d83402740
0x7f9d83402740
0x7f9d83402740

Thanks!