D Language
UFCS - Syntaxe d'appel de fonction uniforme
Recherche…
Syntaxe
- aThirdFun (anotherFun (myFun (), 42); // notation commune (également valide)
- myFun (). anotherFun (42) .aThirdFun (); // UFCS
- myFun.anotherFun (42) .aThirdFun; // les accolades vides peuvent être supprimées
Remarques
Dans un appel ab(args...)
, si le type a
n'a pas de méthode nommée b
, alors le compilateur essaiera de réécrire l'appel comme b(a, args...)
.
Vérifier si un numéro est premier
import std.stdio;
bool isPrime(int number) {
foreach(i; 2..number) {
if (number % i == 0) {
return false;
}
}
return true;
}
void main() {
writeln(2.isPrime);
writeln(3.isPrime);
writeln(4.isPrime);
5.isPrime.writeln;
}
UFCS avec des gammes
void main() {
import std.algorithm : group;
import std.range;
[1, 2].chain([3, 4]).retro; // [4, 3, 2, 1]
[1, 1, 2, 2, 2].group.dropOne.front; // tuple(2, 3u)
}
UFCS avec des durées de std.datetime
import core.thread, std.stdio, std.datetime;
void some_operation() {
// Sleep for two sixtieths (2/60) of a second.
Thread.sleep(2.seconds / 60);
// Sleep for 100 microseconds.
Thread.sleep(100.usecs);
}
void main() {
MonoTime t0 = MonoTime.currTime();
some_operation();
MonoTime t1 = MonoTime.currTime();
Duration time_taken = t1 - t0;
writeln("You can do some_operation() this many times per second: ",
1.seconds / time_taken);
}
Modified text is an extract of the original Stack Overflow Documentation
Sous licence CC BY-SA 3.0
Non affilié à Stack Overflow