I forgot to say that you don't need to write a constructor for most structs because Time's constructor-generated default constructor works like yours and with default arguments:

struct Time {
  public int hours, minutes, seconds;

  // No constructor needed here.

  // Note 'return this;' as the last statement would be
  // mimicing how fundamental types like 'int'
  // work. However, I am getting the following warning when
  // I do that:
  //
  //   Deprecation: returning `this` escapes a reference to
  //   parameter `this` perhaps annotate the function with
  //   `return`
  //
  // For simplicity, I will just return void in this code.
  //
  void opAssign(int secos) {
    assert(secos <= 86_400);
    hours = secos / 3600;
    minutes = (secos % 3600) / 60;
    seconds = secos % 60;
  }
}

import std.stdio;

void main() {
  auto time = Time(360);
  time = 12_345;
  writeln(time);
  readln;
}

Ali

Reply via email to