fork download
  1. import std.exception, std.stdio;
  2.  
  3. struct CustomInteger(T, T minValue, T maxValue)
  4. {
  5. T value_;
  6.  
  7. alias value this;
  8.  
  9. @property T value() const
  10. {
  11. enforce((value_ >= minValue) &&
  12. (value_ <= maxValue));
  13.  
  14. return value_;
  15. }
  16.  
  17. @property void value(T v)
  18. {
  19. value_ = v;
  20. }
  21. }
  22.  
  23. void main()
  24. {
  25. alias Balance = CustomInteger!(int, -32_000, 32_000);
  26.  
  27. auto b = Balance(42);
  28.  
  29. // b += 5; // Error: 'b += 5' is not a scalar,
  30. // it is a CustomInteger!(int, -32000, 32000)
  31.  
  32. writeln(b); // 42
  33.  
  34. b = 40_000;
  35.  
  36. writeln(b); // object.Exception@acm.d(11): Enforcement failed
  37. }
Runtime error #stdin #stdout #stderr 0s 4112KB
stdin
Standard input is empty
stdout
42
stderr
object.Exception@prog.d(11): Enforcement failed
----------------
0x0 libbacktrace could not find executable to open
	???:0
0x807e2dc ???
	???:0
0x804d87d ???
	???:0
0x804e078 ???
	???:0
0x804e02a ???
	???:0
0x804df65 ???
	???:0
0x804d97e ???
	???:0
0x804d8e0 ???
	???:0
0x804cfb3 ???
	???:0
0x80559a0 ???
	???:0
0x8055cbd ???
	???:0
0x8055f00 ???
	???:0
0x8055cbd ???
	???:0
0x8055e38 ???
	???:0
0x804cf5f ???
	???:0
0xb756f722 ???
	???:0
0x804ce65 ???
	???:0