-
-
Save thedeemon/169756a1b9c10283801bc588a8f65f60 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.stdio; | |
// secret sauce: https://dlang.org/spec/class.html#alias-this | |
struct DayNum { | |
int dn; | |
alias dn this; // this line makes DayNum look like an int with dn as the value | |
} | |
DayNum next(DayNum d) { | |
int inty = d + 1; // this is ok, a DayNum can be used as an int | |
return DayNum(inty); // conversion from int to DayNum still needs to be explicit | |
} | |
void main() { | |
auto n = next( DayNum(2) ); | |
//auto n = next( 2 ); // this will not compile, 2 is not a DayNum | |
writeln(n); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Same answer basically: