@property - take it behind the woodshed and shoot it?

Jonathan M Davis jmdavisProg at gmx.com
Sun Jan 27 15:29:47 PST 2013


On Monday, January 28, 2013 00:18:15 Adam D. Ruppe wrote:
> I was just working on some work code and noticed this line that I
> wrote a while ago:
> 
> public @property string userId(string file = __FILE__, size_t
> line = __LINE__) {
> 
> 
> That's a getter, I always use it "getUser(userId);" etc..... but
> it "takes" two arguments.
> 
> If we require getters to have zero arguments, it will break this,
> and that will annoy me. We need to be careful about arbitrarily
> deciding something doesn't make sense and restricting it when
> there's no real technical reason for it.
> 
> 
> (Wondering why I did this? If you aren't logged in, this throws a
> Not Logged In Exception. The file and line are passed to the
> exception constructor, meaning then the error message will tell
> me exactly where I forgot the nicer login check.
> 
> After wasting cumulative hours on similar problems in earlier
> iterations, I put this in there to eliminate that time sink.)

I think that one of two things would be likely to happen were @property to 
stay and properly enforce that it be called with no parens:

1. Your function would be illegal because it had too many arguments.

2. It would be legal, but you could never call it with any arguments for file 
or line except the defaults, because there would be no syntactic way to do so.

But then again, if all you want is a getter and not a setter, and we allow 
optional parens to continue (which appears to be the case), then you wouldn't 
have to declare it as @property to get the syntax that you want. It just 
wouldn't be possible to guarantee that you could swap it out for a variable 
later without breaking code, since it would be possible for someone to call it 
with parens.

- Jonathan M Davis


More information about the Digitalmars-d mailing list