auto ref
Steven Schveighoffer
schveiguy at yahoo.com
Thu Dec 17 12:26:54 PST 2009
On Thu, 17 Dec 2009 15:17:22 -0500, Michel Fortin
<michel.fortin at michelf.com> wrote:
> On 2009-12-17 14:52:40 -0500, "Steven Schveighoffer"
> <schveiguy at yahoo.com> said:
>
>>> The interesting thing about it, beside not taking a keyword, is that
>>> it can scale in the future if we need to add many distinct constness
>>> to the same function signature:
>>> const?(Object) func(const?(Object) o, const?2(Object) o2, out
>>> const?2(Object) o3) {
>>> o3 = o2;
>>> return o;
>>> }
>> This can never work. a const?(Object) is const during the function
>> execution, and cannot be assigned to.
>
> I'm not sure why, but I always forget that const(Object) is not
> rebindable. My mistake, here is the corrected example:
>
> const?(MyStruct)* func(const?(MyStruct)* s, const?2(MyStruct)* s2,
> const?2(MyStruct)* s3) {
> o2 = o3;
> return s;
> }
That doesn't do anything :) It may as well be written:
const?(MyStruct)* func(const?(MyStruct)* s) { return s; }
If you want something like this:
const?(MyStruct)* func(const?(MyStruct)* s, const?2(MyStruct)** s2,
const?2(MyStruct)** s3) {
*s2 = *s3;
return s;
}
This will not accept any args except for const?(MyStruct)** for s2 and
s3. That is, you cannot pass a MyStruct** or a const(MyStruct)** or an
immutable(MyStruct)** because it is 2 levels of indirection (this is the
test we ran earlier).
>
>
>>> Furthermore, the concept could be extended to any type. This could be
>>> useful with class hierarchies:
>>> Object? func(Object? o) {
>>> writeln(o.toString());
>>> return o;
>>> }
>>> MyObject o = func(new MyObject);
>>> Here, "Object?" means Object or a derived type.
>> This doesn't have the same utility as vconst, since you can't apply
>> Object to other types like you can constancy.
>> Plus you can already do this with a template and have a virtual
>> version of the func:
>> T func(T)(T o) if(T : Object) {func_virt(o); return o; }
>> protected void func_virt(Object o) {writeln(o.toString());}
>
> Indeed. I was mostly trying to show that the "const?" notation can
> easily be extended to all sort of things, which makes it a better choice
> than other notations.
>
> We could do the same with an old idea that didn't get in the language:
> scope arguments.
>
> // *a and *b are in the same scope, so you can swap a and b
> void swap(scope?(int)* a, scope?(int)* b) {
> int tmp = a;
> a = b;
> b = tmp;
> }
>
> But the scope problem would require more thought.
OK, that makes more sense. Except scope is not a type constructor (yet) :)
-Steve
More information about the Digitalmars-d
mailing list