On Thu, 30 Dec 2010 05:50:55 -0500, spir <denis.s...@gmail.com> wrote:
Hello,
When 2 constructors (*) accept the same number of parameters, the only
remaining discrimination is type. Right? But some language types (or
machine types) can have very diverse _human_ semantics, and thus be used
for various purposes which should, but cannot, be considered different:
this (int[] data, string filename) {...}
this (int[] data, string message) {...}
Aliasing like in
alias string Name;
does not help since for D Name is still string.
I know about typedef, but it is not even mentionned in TDPL, so I guess
it is on the deprecation path. (Am I right?) So, what is the solution
for this? (I added a 3rd fake bool parameter in one case)
What I would suggest is static factory methods. The issue with any kind
of typedef (be it with the soon-to-be-deprecated typedef keyword or with a
proxy struct), is that what does this mean?
auto obj = new Foo([1, 2, 3], "blah");
Is "blah" a filename or a message?
Whereas, if you use factory methods:
auto obj = Foo.createWithFilename([1,2,3], "blah"); // "blah" is a filename
auto obj = Foo.createWithMessage([1,2,3], "blah"); // "blah" is a message
The code becomes crystal clear. Reduce verbosity as you see fit ;)
I've used this kind of method with creating exceptions in C#, where I want
to generate a message based on the data instead of having to redundantly
specify both the message and the data.
-Steve