<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
13.08.2010 9:37, Yao G. wrote:
<blockquote cite="mid:op.vhc29gqoxeuu2f@miroslava.gateway.2wire.net"
type="cite">Consider this code:
<br>
<br>
---
<br>
module test;
<br>
<br>
struct Foo
<br>
{
<br>
this( int f ) {
<br>
_foo = f;
<br>
}
<br>
<br>
@property int baz() {
<br>
return _foo;
<br>
}
<br>
<br>
// alias _foo this;
<br>
// alias baz this; <br>
<br>
immutable int _foo;
<br>
}
<br>
<br>
struct Bar
<br>
{
<br>
this( int f ) {
<br>
_foo = Foo(f);
<br>
}
<br>
<br>
private:
<br>
immutable Foo _foo;
<br>
}
<br>
---
<br>
<br>
If I uncomment the alias _foo this line, I get the following error
message:
<br>
<br>
% Test.d(22): Error: can only initialize const member _foo inside
constructor
<br>
<br>
WTF! I'm initializing it in a constructor! Is this a bug? Or by
design you cannot alias this to a immutable member of a struct. It
seems that there's a hidden temp created that wants to initialize
the field. Also, I wanted to alias the property Foo.baz, but it
also caused the following errors:
<br>
<br>
% Test.d(22): Error: function test.Foo.baz () is not callable
using argument types (Foo) immutable
<br>
% Test.d(22): Error: expected 0 arguments, not 1 for non-variadic
function type @property int()
<br>
<br>
It seems that somehow the property is used as a "setter", not as a
"getter".
<br>
<br>
So, my questions are:
<br>
1. Why is disallowed to alias this an immutable data inside a
struct?
<br>
2. Why is disallowed to alias this a struct "getter" property?
<br>
<br>
<br>
</blockquote>
If you rename Bar._foo to Bar._fooo, you'll see what it complains
about (yep, about A._foo).<br>
<br>
When you use 'alias x this', methods not defined for 'typeof(this)'
will be searched in typeof(x). In this case, _foo = Foo( f ) is a
constructor call, but...<br>
If alias is present, _foo = Foo(f) translates to
_foo._foo.opAssign(Foo(f)), or, when you alias to baz, to property
assignment (and you indeed don't have a setter).<br>
<br>
Thus being said, I'm not sure if it's a bug or not - seems like an
ambiguity at least.<br>
<br>
<div class="moz-signature">-- <br>
<table style="border: 0px none; font-family: Segoe UI; font-size:
8pt;" width="100%">
<tbody>
<tr>
<td width="50%">
<b><br>
</b>
</td>
<td><br>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>