D Language Super strong compile-time detection technology, strong type technology

Source: Internet
Author: User

The D language introduces static if, static assert, template, is and some other statements, so that D language can use the compile-time to perform logic processing, but also to compile-time testing, which allows the compiler at compile time on the custom class, structure, parameters for dynamic detection, This allows the definition of something with a strong type of function, the work of cow B.

Import Std.stdio; Import Std.range; int Main (string[] argv) {    int[] a = [0,1,2];     int [] b = [4,5,6];     = Chain (A, b);    C.put (new  A ());    Writeln ("C len:", c.length);    Writeln ("C:", c);    Readln ();     return 0;} class a{}

Compile-time prompt:

Click to open the first sentence and navigate to:

This error message is provided by the code, not the compiler. This code is equivalent to extending the compiler, the Cow B bar. Expand # If in C + +, #else宏处理方式.

Make the compiler more intelligent. have been thinking about when C # is going to have compile detection (strongly typed technology), which is a problem at compile time, and not wait until the runtime is a bad thing. This is much more powerful than the detection technique that relies on inheritance relationships and where conditions in C #.

I used to think that in C # where condition compile-time detection technology is very cow, only now found the D language more bull.

At the beginning of reading the "D Language Program" This book, see the static if language, but also just think that is only an # if macro alternative products, did not expect it to have such a powerful power. It's unbelievable. Here's how to implement a type-judging function in a standard library:

Implementation of type judgment function at compile time

This is a compile-time detection function using a template implementation, with 4 detection conditions

template Isinputrange (R) {    enum bool Isinputrange = is (typeof (    int = 0  )    {        = r.init;     // can define a Range object        if (R.empty) {}   //  can test for empty        R.popfront ();     // can invoke Popfront ()        // can get the front of the range     }));}

This detection function can be used not only for conditional detection at compile time, but also for use in programs to determine whether an object implements Inputrange required functions, fields, and properties, which is more powerful than the IS statement that relies only on inheritance in C #.

Read D language for a few days, really more and more like D language, really cow B can't do.

D Language Super strong compile-time detection technology, strong type technology

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.