수색…


통사론

  • aThirdFun (anotherFun (myFun (), 42); // 일반적인 표기법 (유효 함)
  • myFun (). anotherFun (42) .aThirdFun (); // UFCS
  • myFun.anotherFun (42) .aThirdFun; // 빈 괄호는 제거 할 수 있습니다.

비고

ab(args...) 호출에서 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