So much D

This commit is contained in:
NickPapanastasiou 2015-06-10 12:18:45 -04:00
parent 4b35798f83
commit e5fa463b34

View File

@ -53,12 +53,13 @@ void main() {
}
foreach_reverse(i; 1..short.max) {
if(n % 2 == 1)
if(n % 2 == 1) {
writeln(i);
else
} else {
writeln("No!");
}
}
}
```
We can define new types and functions with `struct`, `class`, `union`, and `enum`. Structs and unions
@ -122,3 +123,73 @@ Speaking of classes, let's talk about properties for a second. A property
is roughly a function that may act like an lvalue, so we can
have the syntax of POD structures (`structure.x = 7`) with the semantics of
getter and setter methods (`object.setX(7)`)!
```d
// Consider a class parameterized on a types T, U
class MyClass(T, U) {
T _data;
U _other;
}
// We define "setter" methods as follows
class MyClass(T, U) {
T _data;
U _other;
@property void data(T t) {
_data = t;
}
@property void other(U u) {
_other = u;
}
}
// And "getter" methods like so
class MyClass(T, U) {
T _data;
U _other;
// Constructors are always named `this`
this(T t, U u) {
data = t;
other = u;
}
// getters
@property T data() {
return _data;
}
@property U other() {
return _other;
}
// setters
@property void data(T t) {
_data = t;
}
@property void other(U u) {
_other = u;
}
}
// And we use them in this manner
void main() {
auto mc = MyClass!(int, string);
mc.data = 7;
mc.other = "seven";
writeln(mc.data);
writeln(mc.other);
}
```
With properties, we can add any amount of validation to
our getter and setter methods, and keep the clean syntax of
accessing members directly!