Why people dislike global variables so much while I find them so convenient?

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Jan 26 15:36:52 UTC 2022


On Wed, Jan 26, 2022 at 08:46:04AM +0000, rempas via Digitalmars-d wrote:
> On Tuesday, 25 January 2022 at 14:28:24 UTC, H. S. Teoh wrote:
[...]
> > Either group your parameters in a single struct, or make your
> > functions members of the struct. Then all you have to do is add
> > another field to your struct and you're done. No tedium.
> 
> Actually, I was wondering something. Maybe I should read the whole
> reference first but I'll just ask in case you know. Is there a way to
> just declare a struct/class method and define it outside the
> struct/class like in C++? It will be very annoying to have all my
> functions be in the same file. The first method seems better to me.
[...]

Thanks to UFCS, you don't need to define every function that operates on
the struct/class as a method. Free functions work just fine.

I.e., instead of this:

	struct MyData {
		int x;
		void method() {
			x++;
		}
	}

	MyData data;
	data.method();

you could also write this:

	struct MyData {
		int x;
	}

	void method(ref MyData mydata) {
		mydata.x++;
	}

	MyData data;
	data.method();


T

-- 
Debian GNU/Linux: Cray on your desktop.


More information about the Digitalmars-d mailing list