Owned members
bearophile
bearophileHUGS at lycos.com
Sat Dec 25 01:06:15 PST 2010
I am far from being an expert C++ (or OOP) programmer still, so probably some of my needs look (or are) naive.
This looks related to the old discussion of a 'scoped' attribute for class members, but it's a different thing, more about the semantics of the code and less about how the compiler implements things.
Sometimes I feel the need for something (usable on class/struct/enum members) to tag a member as "owned" by the class instance. This attribute is useful only for reference attributes, like objects, structs managed by pointer, associative arrays and dynamic arrays. This attribute is purely a type system thing, the compiled code doesn't change. It just disallows some kinds of code, shown below. The purpose of @owned is to hopefully avoid some bugs.
class Foo {}
class Bar {
@owned private int[] array;
@owned private f Foo;
public int[] giveArray() {
return array; // error, a dup is required
}
public void receiveArray(int[] a) {
array = a; // error, a dup is required
}
public Foo giveFoo() {
return f; // error
}
public void receiveFoo(Foo ff) {
f = ff; // error
}
}
If array is owned by Bar, then "return array;" is a compile-time error because the memory of the array is only owned by Bar; returning a slice or reference is not allowed.
Is it possible to implement this @owned, and is it a good thing to have?
Bye,
bearophile
More information about the Digitalmars-d
mailing list