| |
| Posted by Ali Çehreli (acehreli) in reply to rkaratas | PermalinkReply |
|
Ali Çehreli (acehreli)
Posted in reply to rkaratas
| Kod kaybolmaz! :p
import std.format;
import std.stdio;
/**
Bu islev sunu yapar
*/
string parantezli(string s) {
return format!"(%s)"(s);
}
///
unittest {
assert(parantezli("hello") == "(hello)");
}
void main() {
writeln("merhaba".parantezli);
}
import std.exception;
//import std.range;
import std.array;
struct Foo {
private:
string s;
public:
this(string s) {
this.s = s;
}
/+
Throwable
/ \
Error Exception
+/
bool aynı_mı(string s)
in (!s.empty, "olmaz") // Error: dokunma!
{
enforce(!s.empty); // Exception: catch edilebilir
return s == this.s;
}
}
unittest {
auto f = Foo("hello");
assert(f.aynı_mı("hello") == true);
assert(f.aynı_mı("merhaba") == false);
assertThrown(f.aynı_mı(""));
assertNotThrown(f.aynı_mı("aostehas"));
}
void main() {
}
struct Bar {
this(int i) {
auto f = Foo("hello");
f.s = "merhaba";
}
}
import std.stdio;
struct Dörtgen {
int en;
int boy;
int alan;
private void ayarla() {
alan = en * boy;
}
this(int en, int boy) {
this.en = en;
this.boy = boy;
ayarla();
}
invariant() {
assert(alan == en * boy);
}
void büyült() {
en *= 2;
boy *= 2;
ayarla();
}
// mutable, const, immutable için:
void yazdır() const {
writeln("const: ", en, boy);
}
void yazdır() immutable {
writeln("immutable: ", en, boy);
}
void yazdır() shared {
}
}
shared int i; // deneme.i x kaç thread işliyorsa
// immutable is implicitly shared
// string parantezli(string s, int[] a, int[] b)
// in (!s.empty)
// out(result; result.length >= 3)
// out (result; result.length == a.length + b.length)
// {
// return format!"(%s)"(s);
// }
void main() {
auto d = Dörtgen(10, 20);
d.büyült();
d.yazdır();
const d2 = Dörtgen(1, 2);
d2.yazdır();
immutable d3 = Dörtgen(7, 8);
d3.yazdır;
}
void foo(const int[] arr) {
// arr'de değişiklik yapmayacağıma söz veriyorum
}
void bar(immutable int[] arr) {
// bana vereceğin dizi immutable olmalı; buna güveniyorum
kilitlemeden_başka_thread_e_gönder(arr);
}
Ali
--
[ Bu gönderi, http://ddili.org/forum'dan dönüştürülmüştür. ]
|