vectorgraphics / asymptote

2D & 3D TeX-Aware Vector Graphics Language
https://asymptote.sourceforge.io/
GNU General Public License v3.0
556 stars 93 forks source link

Feature suggestion: automatic generation of constructor #447

Open user202729 opened 6 months ago

user202729 commented 6 months ago

For example, currently in order to define a constructor we need to

struct Person {
  string firstname;
  string lastname;

  static Person Person(string firstname, string lastname) {
    Person p=new Person;
    p.firstname=firstname;
    p.lastname=lastname;
    return p;
  }
}

Or a bit shorter:

struct Person {
  string firstname;
  string lastname;

  void operator init(string firstname, string lastname) {
    this.firstname=firstname;
    this.lastname=lastname;
  }
}

But I think that can be automatically generated by something like the following (new syntax)

struct Person {
  string firstname;
  string lastname;

  void operator init=auto;
}

Inspired by =default; and =delete; syntax from C++.

charlesstaats commented 6 months ago

Nice idea. There are a couple potential issues with the idea as suggested:

  1. What about fields defined after void operator init = auto;? Are they included or not? In Asymptote (unlike modern C++ or even Python), the compiler generally doesn't have access to anything that hasn't happened later in the file. Technically speaking this could be worked around, but someone will find it confusing either way. I can also see a version of this in which multiple "default" constructors are allowed; so, for instance,
    struct Person {
    string firstname;
    string lastname;
    void operator init=auto;
    Person[] children;
    void operator init=auto;
    }

    might have two "default" constructors, one of which takes (firstname, lastname) while the other takes (firstname, lastname, children).

  2. Normally, in Asymptote (unlike C++), a method is a kind of field. So which fields should be included in the following?
    struct Info {
    string name;
    void addname(string s) { name += s; }
    arrowbar3 ab;
    void operator init = auto;
    }

    Presumably we want to set ab but not addname in the default constructor. But note that, by the definition of arrowbar3, the above is equivalent to this:

    struct Info {
    string name;
    void addname(string s) { name += s; }
    bool ab(picture, path3, material, margin3, light, light);
    void operator init = auto;
    }

    And in this iteration, it's less clear that ab should be a constructor parameter.

One idea to solve the latter issue would be that the constructor should reference those variables that are not explicitly initialized. So for instance, in

struct Person {
  string firstname;
  string lastname = 'Smith';
  void operator init = auto;
}

firstname but not lastname would be parameters of the default initializer. On the other hand, should those explicitly initialized variables be parameters that have default values? It would also add a bit of complexity to the compiler; at the moment, there's no attempt made at compile time to keep track of whether or not a variable has been initialized.

None of this is to say that the idea cannot or should not be implemented in some form, only that careful thought is required.

One final note: While suggested improvements are always appreciated, please do keep in mind that many improvements can sit around for years before anyone gets around to implementing them (if ever), even if the improvement is straightforward and everyone agrees on what it ought to be.