Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Struct "constructor" using opCall

Part of TutorialAdvanced

Description

Define a constructor-like function for structs

Example

struct Foo {
  int a;
  static Foo opCall(int a) {
    Foo s;
    s.a = a;
    return s;
  }
}

int main() {
  Foo x = Foo(10);
  writef("%d\n", x.a);
  return 0;
}

Source

Link http://www.dsource.org/tutorials/index.php?show_example=70
Posted by jcc7
Date/Time Thu May 20, 2004 11:21 pm