On Mon, 15 Jul 2013 20:06:38 +0100, Meta <[email protected]> wrote:

I saw an interesting post on Hacker News about constructors in OO languages. Apparently they are a real stumbling block for some programmers, which was quite a surprise to me. I think this might be relevant to a discussion about named parameters and whether we should ditch constructors for another kind of construct.

Link to the newsgroup post, the link to the paper is near the top:
http://erlang.org/pipermail/erlang-questions/2012-March/065519.html

First thought; constructors with positional arguments aren't any different to methods or functions with positional arguments WRT remembering the arguments. The difficulties with one are the same as with another - you need to remember them, or look them up, or get help from intellisense.

I think the point about constructed objects being in valid states is the important one. If the object requires N arguments which cannot be sensibly defaulted, then IMO they /have/ to be specified at construction, and should not be delayed as in the create-set-call style mentioned.

Granted, A create-set-call style object could throw detailed/useful messages when used before initialisation, but that's a runtime check so IMO not a great solution to the issue.

Also, I find compelling the issue that a create-set-call style object with N required set calls could be initialised N! ways, and that each of these different orderings have effectively the same semantic meaning.. so it becomes a lot harder to see what is really happening. Add to that, that someone could interleave the initialisation of another object into the first and .. well .. shudder.

So, given the desire to have objects constructed in a valid state, and given the restriction that this may require N arguments which cannot be defaulted how do you alleviate the problem of having to remember the parameters required and the ordering of those parameters?

Named parameters only help up to a point. Like ordered parameters you need to remember which parameters are required, all that has changed is that instead of remembering their order you have to remember their names. So, IMO this doesn't really solve the problem at all.

A lot can be done with sufficiently clever intellisense in either case (ordered/named parameters), but is there anything which can be done without it using just a text editor and compiler?

Or, perhaps another way to ask a similar W is.. can the compiler statically verify that a create-set-call style object has been initialised, or rather that an attempt has at least been made to initialise all the required parts.

We have class invariants.. these define the things which must be initialised to reach a valid state. If we had compiler recognisable properties as well, then we could have an initialise construct like..

class Foo
{
  string name;
  int age;

  invariant
  {
    assert(name != null);
    assert(age > 0);
  }

  property string Name...
  property int Age...
}

void main()
{
  Foo f = new Foo() {
    Name = "test",    // calls property Name setter
    Age = 12          // calls property Age setter
  };
}

The compiler could statically verify that the variables tested in the invariant (name, age) were set (by setter properies) inside the initialise construct {} following the new Foo().

R

--
Using Opera's revolutionary email client: http://www.opera.com/mail/

Reply via email to