サーチ…


構文

  • aThirdFun(anotherFun(myFun()、42); //共通表記(有効)
  • myFun()。anotherFun(42).aThirdFun(); // UFCS
  • myFun.anotherFun(42).aThirdFun; //空の中括弧は削除できます

備考

コールではab(args...)タイプの場合、 aという名前のメソッドはありませんb 、コンパイラはとしてコールを書き換えしようとするb(a, args...)

番号がプライムであるかどうかをチェックする

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

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)        
}

std.datetimeからの継続時間を持つUFCS

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
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow