discrimination of constructors with same number of parameters
spir
denis.spir at gmail.com
Thu Dec 30 09:08:56 PST 2010
On Thu, 30 Dec 2010 17:10:00 +0100
"Jérôme M. Berger" <jeberger at free.fr> wrote:
> Steven Schveighoffer wrote:
> > 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?
> >
> --> Error, Foo (int[], string) does not exist.
Yes, you are right. Typedef-like solutions need core support by the language with a kind of hint to the compiler... playing the role of type in Jérôme's sample below.
> > 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
Factory methods are definitely convenient. The single objection is rather conceptual: it defeats the purpose of a major language feature, namely constructor; which happens to have a clear meaning from the modelling point of view.
> > The code becomes crystal clear. Reduce verbosity as you see fit ;)
> >
> auto obj = new Foo ([1, 2, 3], Filename ("blah"));
> auto obj = new Foo ([1, 2, 3], Message ("blah"));
Conceptually, I would prefere this -- at the use place. But if requires obfuscating the code at the definition point (with eg wrapper structs), is it worth it?
If we could write eg:
typedef string Message;
auto obj = new Foo ([1, 2, 3], Message ("blah"));
then I would be happy, I guess ;-)
Denis
-- -- -- -- -- -- --
vit esse estrany ☣
spir.wikidot.com
More information about the Digitalmars-d-learn
mailing list