| Thread overview | |||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 01, 2008 getAttr method | ||||
|---|---|---|---|---|
| ||||
Hello, I would like to suggest an feature to the language :
Every time we try to access a member of an object that has no such member, the compiler try to call a special method with the member name as a template parameter.
This would be similar to the __getattr__ method in python, and it can be useful sometime.
Ex:
class Test {
string getAttr(string name)() {return name;}
}
void main() {
Test t = Test();
writefln(t.hello); // write "hello"
}
- Guillaume
| ||||
February 01, 2008 Re: getAttr method | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Guillaume Chéreau | Guillaume Chéreau:
>and it can be useful sometime.
Can you show one or more (practical) examples of situations where it can be useful?
Bye,
bearophile
| |||
February 01, 2008 Re: getAttr method | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Guillaume Chéreau | On Feb 1, 2008 9:09 AM, Guillaume Chéreau <charlie137@gmail.com> wrote:
> Hello, I would like to suggest an feature to the language :
>
> Every time we try to access a member of an object that has no such member, the compiler try to call a special method with the member name as a template parameter.
I'd prefer that if I misspell a member name, the compiler should point out my error, and fail to compile the source, with an indication of the file and line number where the error occurred. (Current behaviour, in other words).
| |||
February 01, 2008 Re: getAttr method | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | Janice Caron escribió:
> On Feb 1, 2008 9:09 AM, Guillaume Chéreau <charlie137@gmail.com> wrote:
>> Hello, I would like to suggest an feature to the language :
>>
>> Every time we try to access a member of an object that has no such member, the compiler try to call a special method with the member name as a template parameter.
>
> I'd prefer that if I misspell a member name, the compiler should point
> out my error, and fail to compile the source, with an indication of
> the file and line number where the error occurred. (Current behaviour,
> in other words).
>
It will, only if it can't find the getAttr method.
| |||
February 01, 2008 Re: getAttr method | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Ary Borenszweig | Ary Borenszweig schrieb:
> Janice Caron escribió:
>> On Feb 1, 2008 9:09 AM, Guillaume Chéreau <charlie137@gmail.com> wrote:
>>> Hello, I would like to suggest an feature to the language :
>>>
>>> Every time we try to access a member of an object that has no such member, the compiler try to call a special method with the member name as a template parameter.
>>
>> I'd prefer that if I misspell a member name, the compiler should point
>> out my error, and fail to compile the source, with an indication of
>> the file and line number where the error occurred. (Current behaviour,
>> in other words).
>>
>
> It will, only if it can't find the getAttr method.
Probably a "static assert" could catch unwanted calls at compile time.
| |||
February 01, 2008 Re: getAttr method | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | bearophile wrote: >>and it can be useful sometime. > > Can you show one or more (practical) examples of situations where it can > be useful? PHP has something similar. These sorts of features seem to be used to make classes do magic things and be harder to understand. But just this feature for D? Well, I suppose you could use it to print something like: "Test has no function called 'hello', silly programmer!" Ehm.. at runtime, it would seem. ;-) -- Michiel | |||
February 01, 2008 Re: getAttr method | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Michiel Helvensteijn | Michiel Helvensteijn:
> PHP has something similar.
I use that in Python, so I know that (once in while) it can be useful in Python code.
But what I don't understand how much good it can be in a static language like D. I think Python has many other features that can be more useful for D (likes list comprehensions, generators, and few other syntactically sugared things, that ShedSkin translates to C++ quite well).
Bye,
bearophile
| |||
February 01, 2008 Re: getAttr method | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | bearophile Wrote: > Guillaume Chéreau: > >and it can be useful sometime. > > Can you show one or more (practical) examples of situations where it can be useful? It could be useful to create 'decorator' objects. That is an object that encapsulate an other object and redirect all the methods except a particular one. This can be used for iterators. ex : (I will suppose we also have a function glob_getAttr(o)(name) that calls the method with the given name of the object o) Class Decorator(T) { T obj; this(T obj) {this.obj = obj;} typeof(glob_getAttr!(T)(name)) getAttr(string name)() { return glob_getAttr(obj, name); } int x() {return 2 * obj.x();} } void main() { A a = new A(); auto d = new Decorator!(A)(a); d.f // return a.f d.x // return 2 * a.x } OK, as I write it i realize how more useful and simple this kind of things are in python than in compiled language. Maybe it is not as good an idea as I though after all :) Guillaume > > Bye, > bearophile | |||
February 01, 2008 Re: getAttr method | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | bearophile wrote: > Michiel Helvensteijn: >> PHP has something similar. > > I use that in Python, so I know that (once in while) it can be useful in Python code. > But what I don't understand how much good it can be in a static language like D. I think Python has many other features that can be more useful for D (likes list comprehensions, generators, and few other syntactically sugared things, that ShedSkin translates to C++ quite well). > Well, generators are available as add-on libraries; StackThreads form a superset of them :) the following code is entirely valid D if you have scrapple.tools installed: auto generator = stackthread = (void delegate(int) yield) { int i; while (true) yield(i++); }; writefln(generator(), generator(), generator()); Except that probably won't work on a stock phobos, because win32's MmFile is broken (patch in bugzilla), and the GC can be made to work with StackThreads but it's horribly inefficient, which is why scrapple.tools currently only works with the GC patch posted above. Sorry. But hey, maybe it or a similar patch will eventually be integrated into phobos! :cue laugh track: Yeah I know. But I still have a few specks of hope left. Call me stupid :) > Bye, > bearophile --downs | |||
February 01, 2008 Re: getAttr method | ||||
|---|---|---|---|---|
| ||||
Posted in reply to downs | downs Wrote: > Except that probably won't work on a stock phobos, because win32's MmFile is broken (patch in bugzilla), and the GC can be made to work with StackThreads but it's horribly inefficient, which is why scrapple.tools currently only works with the GC patch posted above. I see. > Yeah I know. But I still have a few specks of hope left. Call me stupid :) I like people that have hope still. I think I'll eventually drop Phobos and I'll start using Tango, despite some things I don't like of it (like the too much long dotted names, its source code being less readable, and other things), because I think it's already better debugged, a bit more open to external contributors, and I think with time it will become good enough :-) Bye, bearophile | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply