October 21, 2006 Re: One way to look at foreach as not so bad | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter wrote: > Julio César Carrascal Urquijo wrote: >> I don't understand your point. You can already write something like: >> >> obj.each = (int i) { >> writefln(i); >> }; >> > > How do you set that up? Something like this? > > class ObjClass > { > class EachClass { > void opAssign( void delegate(...) ) { > [...] > } > } > EachClass each = new EachClass(); > } > ObjClass obj = new ObjClass(); That won't work. There is no opAssign. > Or is there a better way? In the sense that it works, yes. Something like this: class ObjClass { void each(void delegate(...) ) { [...] } } ObjClass obj = new ObjClass(); This somewhat abuses the property syntax, but should result in the syntax above. |
October 21, 2006 Re: One way to look at foreach as not so bad | ||||
---|---|---|---|---|
| ||||
Posted in reply to Julio César Carrascal Urquijo | Julio César Carrascal Urquijo wrote:
> Tom S wrote:
> > Yup, or
>>
>> obj.each in (int i) {
>> writefln(i);
>> };
>
> Well yes, "in" looks better but it doesn't work right now, "=" does.
Sure it works ! :)
import std.stdio;
class Foo {
int[] data;
struct Iter_each {
Foo owner;
void opIn(void delegate(int) dg) {
foreach (int d; owner.data) {
dg(d);
}
}
}
Iter_each each() {
Iter_each res;
res.owner = this;
return res;
}
}
void main() {
Foo foo = new Foo;
foo.data = [34, 2, 55, 23];
foo.each in (int i) {
writefln(i);
};
}
--
Tomasz Stachowiak
|
October 22, 2006 Re: One way to look at foreach as not so bad | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frits van Bommel | Frits van Bommel wrote:
> Bill Baxter wrote:
>
>> Julio César Carrascal Urquijo wrote:
>>
>>> I don't understand your point. You can already write something like:
>>>
>>> obj.each = (int i) {
>>> writefln(i);
>>> };
>>>
>>
>> How do you set that up? Something like this?
>>
>> class ObjClass
>> {
>> class EachClass {
>> void opAssign( void delegate(...) ) {
>> [...]
>> }
>> }
>> EachClass each = new EachClass();
>> }
>> ObjClass obj = new ObjClass();
>
>
> That won't work. There is no opAssign.
Oh yeh, that's right. :-) I think "opAssign" must have looked familar to me merely because of previous discussions about its lack.
--bb
|
Copyright © 1999-2021 by the D Language Foundation