D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 1123 - We can change Struct.init
Summary: We can change Struct.init
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2007-04-11 11:06 UTC by s180m
Modified: 2012-01-29 19:32 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description s180m 2007-04-11 11:06:06 UTC
struct Matrix {
  double e = 1.;

  Matrix scale(double r){e*=r;return *this;}

  const Matrix Identity = {10.};
}

void main(){
  for(int i;i<5;i++){
    printf("%f\n",Matrix.Identity.scale(.9).e);
  }
  printf("\n");
  for(int i;i<5;i++){
    printf("%f\n",Matrix.init.scale(.9).e);
  }
}
-----------------
9.000000
8.100000
7.290000
6.561000
5.904900

0.900000
0.810000
0.729000
0.656100
0.590490
Comment 1 Thomas Kühne 2007-04-25 12:58:15 UTC
That is again a case where the compiler didn't put unchangeable content 
(Identitiy and init) readonly section but read-write section. On Linux "init" 
is in a readonly section and thus the second loop segfaults.

It would be nice if the compiler enforced/checked read-write rights.
Comment 2 yebblies 2012-01-29 19:32:41 UTC
Currently both D1 and D2 (1.068 & 2.058) compilers print:
9.000000
9.000000
9.000000
9.000000
9.000000

0.900000
0.900000
0.900000
0.900000
0.900000

As .init creates a temporary this is correct.