Blog post: What D got wrong

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Dec 11 18:52:07 UTC 2018


On Tue, Dec 11, 2018 at 12:57:03PM +0000, Atila Neves via Digitalmars-d-announce wrote:
> On Tuesday, 11 December 2018 at 12:52:20 UTC, Adam D. Ruppe wrote:
> > On Tuesday, 11 December 2018 at 10:45:39 UTC, Atila Neves wrote:
> > > A few things that have annoyed me about writing D lately:
> > > 
> > > https://atilanevesoncode.wordpress.com/2018/12/11/what-d-got-wrong/
> > 
> > If @property worked for a thing to return a delegate, it would be
> > useful.
> > 
> > But noooo, we got worked up over syntax and forgot about semantics
> > :(
> 
> @property is useful for setters. Now, IMHO setters are a code stink
> anyway but sometimes they're the way to go. I have no idea what it's
> supposed to do for getters (nor am I interested in learning or
> retaining that information) and never slap the attribute on.

You don't need @property for setters. This works:

	struct S {
		void func(int x);
	}
	S s;
	s.func = 1;

Of course, it's generally not a good idea to call it `func` when the
intent is to emulate a member variable. :-D

I agree setters are a code stink, but only when they are trivial:

	struct S {
		private int _x;

		// This is a code smell: just make _x public, dammit!
		void x(int val) { _x = val; }
	}

But they can be very useful for non-trivial use cases.  Recently I wrote
code that auto-generates a nice D API for setting GLSL inputs. So
instead of writing:

	FVec position;
	glUniform3fv(myshader.u_dirLightId_pos, 1, position[].ptr);

I write:

	FVec position;
	myshader.position = position; // much more readable and less error prone!

with myshader.position defined as a setter function that does that ugly
glUniform3fv call for me. Plus, I can hide away that ugly internal
detail of attribute position IDs and make it private, instead of
exposing it to the world and adding a needless GL dependency to client
code. (E.g., now I have the possibility of substituing a Direct3D
backend for the OpenGL just by emitting a different implementation for
myshader.position. The calling code doesn't need to be touched.)


T

-- 
If you think you are too small to make a difference, try sleeping in a closed room with a mosquito. -- Jan van Steenbergen


More information about the Digitalmars-d-announce mailing list