Does D have too many features?

Alex Rønne Petersen xtzgzorex at gmail.com
Mon Apr 30 10:14:00 PDT 2012


On 30-04-2012 19:05, H. S. Teoh wrote:
> On Mon, Apr 30, 2012 at 06:54:31PM +0200, bearophile wrote:
>> H. S. Teoh:
>>
>>> Which means your code is at the mercy of the external library.
>>> Upstream updates a class, and suddenly a whole bunch of code is
>>> unnecessarily broken
>>
>> How? (I think you are wrong again).
> [...]
>
> 	struct S {
> 		int x;
> 	}
> 	void main() {
> 		int y;
> 		S s;
>
> 		with(s) {
> 			x = 1;
> 			y = 2;
> 		}
> 	}
>
> This works. Now suppose S is updated to:
>
> 	struct S {
> 		int x;
> 		int y;
> 	}
>
> Now the program fails to compile because S.y conflicts with the local y.
>
> This is bad because unrelated code is broken just by changing S: it
> breaks encapsulation. This is just a small example; imagine if a lot of
> code uses S. Many places may break when S changes just because they
> happen to use the wrong local variable names.
>
> Whereas if you had _not_ used with, this is a non-problem, since you'd
> be referring to s.x, and the fact that S now has a new member does not
> break any existing code regardless of how it was named.
>
>
> T
>

You always risk breaking *something* when you change an interface, 
whether it's through adding or removing members. The problem you point 
out is not at all specific to with. Consider UFCS: You have struct S and 
free function foo operating on S. Upstream now adds a method foo to S.

-- 
- Alex


More information about the Digitalmars-d mailing list