On Friday, 2 September 2022 at 18:35:22 UTC, Svyat wrote:
I write this code in one directory:
```
module time;
struct Time {
public int hours, minutes, seconds;
this(int h, int m, int s) {
hours = h;
minutes = m;
seconds = s;
}
On Friday, 2 September 2022 at 18:39:27 UTC, rikki cattermole
wrote:
I think you are wanting opAssign not opBinary.
Also you made a mistake, since its a struct you don't want to
new it when you construct and return it.
```d
return new Time(secos / 3600, (secos % 3600) / 60, secos % 60);
```
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
On 9/2/22 11:35, Svyat wrote:
> Time time = 360;
It seems to be more idiomatic to write it like this:
auto time = Time(360);
Or const, immutable, etc.
const time = Time(360); // Now un-assignable
But you would get the same compilation error. So, one way to work with
it is to
I think you are wanting opAssign not opBinary.
Also you made a mistake, since its a struct you don't want to new it
when you construct and return it.
```d
return new Time(secos / 3600, (secos % 3600) / 60, secos % 60);
```
Would be a ``Time*`` not ``Time`` which is what you returned.
I write this code in one directory:
module time;
struct Time {
public int hours, minutes, seconds;
this(int h, int m, int s) {
hours = h;
minutes = m;
seconds = s;
}
Time opBinary(string op : "=")(int secos) {