bearophile wrote:
Chad J Wrote:
This is exactly where I'm coming from. I used to use C# properties a
lot. They are super effective.
In C# you can use for example:
class TimePeriod {
private double seconds;
public double Hours {
get { return seconds / 3600; }
set { seconds = value * 3600; }
}
}
You can also write it in native C++ using microsoft extension:
// declspec_property.cpp
struct S {
int i;
void putprop(int j) {
i = j;
}
int getprop() {
return i;
}
__declspec(property(get = getprop, put = putprop)) int the_prop;
};
int main() {
S s;
s.the_prop = 5;
return s.the_prop;
}
Or just:
public double TotalPurchases { get; set; }
Some people have proposed:
public int property Myval {
get;
set {
if (value > 10)
throw new Exception();
else
Myval = value;
}
}
Time ago I have written this for D1, I don't know if it can be useful:
import std.metastrings: Format;
template AttributeGetSet(Type, string name) {
const AttributeGetSet = Format!("
private %s %s__;
public %s %s() { return this.%s__; }
public void %s(int %s__local) { this.%s__ = %s__local; }
", Type.stringof, name, Type.stringof, name, name, name, name, name, name);
}
C# also has indexers:
Indexers allow instances of a class or struct to be indexed just like arrays.
Indexers resemble properties except that their accessors take parameters.<
http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx
Usage example:
class SampleCollection<T> {
private T[] arr = new T[100];
public T this[int i] {
get {
return arr[i];
}
set {
arr[i] = value;
}
}
}
But to me that looks a lot like the opIndex/opIndexAssign/opIndexLvalue of D.
Bye,
bearophile
Finally I don't think very relevant to have a D.Net because people doing
.NET want to have Microsoft support, and it will never be used in real
production software.
The only advantage I see is to talk more about D that is for now quite
discreet.