Previous Entry Share Next Entry
Explaining Language Design (Part 5: Type Inference)
anime
he_the_great

Up next in "The Last Thing D Needs" is type deduction when using the many ways to initialize an integer. Simply put, D has one way. However there are a few things to go over.

C++
int x2(0);
int x4 { 0 };

Let me start by getting this out of the way. These aren't valid in D.

dlang
version(noneint x3 = { 0 };
version(noneauto x3 = { 0 };

Neither of these compile, there is a type mismatch which happens. D supports the C style initialization for structures. While it is nice that D tries to compile C code, I'm not sure that it has gained much from this. The support for static array syntax, I think causes problems.

If you'll recall from Part 4, D utilizes braces to create lambda. This is still true, but the compiler doesn't see a statement inside, if we place a semicolon after 0 it will complain that long has no effect.

dlang
void main() {
    int[4][10] a1;
    int a2[10][4];
    assert(is(typeof(a1) == typeof(a2)));
}

Please don't write the second form. If it came from C, fine, but don't write it.

Conclusion

Due to the attempt to compile C code D provides a number of options for some operations. This causes some overlap with syntax it has chosen for other things, and can cause confusion since C does not always provide the clearest syntax.

  1. Part 1: Default Initialization
  2. Part 2: Const Inference
  3. Part 3: Lambdas in C++
  4. Part 4: Lambdas in D
  5. Part 5: Type Inference
  6. Part 6: Inheritance
  7. Part 7: Algorithm Complexity
  8. Part 8: Essential Complexity

?

Log in