[phobos] Time to get ready for the next release

Steve Schveighoffer schveiguy at yahoo.com
Fri Apr 22 12:26:51 PDT 2011


Actually, I think you are right.  I wasn't thinking about dmd erroring on a statement that does nothing.  You'd have to assign something to the expression, like:

auto x = s.seconds = 5;

Which still looks like it does something else, but is much less likely to occur.


You wouldn't need the parameter to be immutable, because the parameter is a value, making this a strong-pure function.


But this is still not an argument against strict properties.  I only bring up this example because it's the only case I've had where I had to change code due to a complaint that someone could use a function as a property.  Most cases you just say "sorry, the code isn't meant to be used that way, RTFM." But it would be better to avoid having the user complain in the first place.  It certainly gives a more polished feel for your library when you don't leave syntax holes like this open.

I look at the way a property/function is called as part of the 'human interface', i.e. it's part of the name of the function/property.  If you cannot control the human interface, you cannot control what the human thinks when he reads it.  Strict properties are all about freedom for the author to convey the correct means of using the library.  The author's only tool to enforce the interface is the compiler.  It's in the same vein as static typing -- yes, it's restrictive, but restriction gives you less room for errors.

Another analogy I like to draw upon is casing.  What if D's casing was insensitive?  That is, ReadValue is the same thing as readValue and readvalue .  There are probably many people who would love to always use their learned conventions for calling your code (e.g. I always make methods upper case), but then someone comes along and types in reAdvalue (my super-uncreative brain can't come up with a clever example to show something worse, but you get the idea).  The name is the same, but the casing makes all the difference to interpreting what it means!  Like it or not, the same thing applies to things like:

writeln = "hello";

Even though we know this is not the right way to call it, the compiler doesn't give an error to enforce the semantics.


-Steve



----- Original Message -----
> From: Robert Jacques <sandford at jhu.edu>
> To: Discuss the phobos library for D <phobos at puremagic.com>
> Cc: 
> Sent: Friday, April 22, 2011 2:55 PM
> Subject: Re: [phobos] Time to get ready for the next release
> 
> On Fri, 22 Apr 2011 14:20:21 -0400, Robert Jacques <sandford at jhu.edu> 
> wrote:
> 
>>  On Fri, 22 Apr 2011 13:22:31 -0400, Steve Schveighoffer 
> <schveiguy at yahoo.com> wrote:
>>>>  From: Robert Jacques <sandford at jhu.edu>
>>  [snip]
>>>>  Forth, came the realization that in D2 'seconds' would 
> probably be pure, which would cause s.seconds = 5 to be compiler error.
>>> 
>>>  No, it wouldn't be an error.  s.seconds(5) is exactly the same as 
> TimeSpan.seconds(5), both would be callable as pure functions.  In other words, 
> s isn't actually passed to the function, it's just used as a namespace.
>> 
>>  I apologize, pure alone wouldn't be enough to cause a compiler error. I 
> was thinking of the fact that DMD could/does error on expressions which do 
> nothing. However, pure wouldn't be enough to let the compiler know 
> 'seconds = 5' does nothing. It would probably take a combination of 
> const/immutable + pure + nothrow, for DMD to detect that s.seconds = 5 could not 
> possible produce useful work, and therefore should error.
> 
> P.S. On third thought, you'd really only need a const/immutable method, as 
> assignment of const/immutable variables is invalid. (This would require a 
> heuristic in DMD, etc). Also, given we can have static variables, static methods 
> should also be able to be const/immutable. But since static variables are 
> considered global state, perhaps pure covers those use cases. Hmm... so maybe I 
> was correct in that a strongly-pure function called with field syntax should 
> always be an error.
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
>


More information about the phobos mailing list