July 06, 2021

On Tuesday, 6 July 2021 at 15:24:37 UTC, jfondren wrote:

>
  1. https://run.dlang.io/is/AJM6Vg - hybrid where ClockAssign has an unsafe pointer that the compiler complains about :/
import std.stdio;

struct Field {
    void opAssign(int a) {
        writefln("Field.opAssign(%s)", a);
    }
}

struct ClockAssign {
    Field[] clocks;
    void opIndexAssign(int a, int b) {
        writefln("Register.clock(%s)", a);
        clocks ~= Field();
        clocks[$-1] = b;
    }
}

struct Register {
    ClockAssign clock;
}


void main() {
    Register register;
    register.clock[1] = 10;
    // register.clock = 10; // error
}
July 07, 2021

On Tuesday, 6 July 2021 at 10:06:11 UTC, Jack Applegame wrote:

>

Here's another way using std.typecons

import std.stdio;
import std.typecons;
alias input = Typedef!int; //new code
struct Field {
    void opAssign(int a) {
        writefln("Field.opAssign(%s)", a);
    }
}

struct Register {
    Field clock(input a) {
        writefln("Register.clock(%s)", cast(int)a); //note the cast
        return Field();
    }
}


void main() {
    input ip = 1;
    Register register;
    register.clock(ip) = 10; // works, good
    //register.clock = 10;    // works too, how to disable it?    EDIT: Now it doesn't work :)
}

You will have to either cast your literal, or provide a variable with the new type as input though, so it is also not perfect.

1 2
Next ›   Last »