www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - opApply outside of struct/class scope

reply "Freddy" <Hexagonalstar64 gmail.com> writes:
I'm trying to implement a opApply outside of struct scope
----
struct A{
	int[] arr;
}
int opApply(ref A a,int delegate(ref int) dg){
	return 0;
}
void main(){
	A a;
	foreach(i;a){//i just want it to compile
		
	}
}
----
when i try compiling, the compiler errors:
   test.d(9): Error: invalid foreach aggregate a
Is there any why i can put a opApply outside of a struct scope,if
so is there any why that the opApply can be templated
Aug 10 2014
parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Sunday, 10 August 2014 at 18:45:00 UTC, Freddy wrote:
 Is there any why i can put a opApply outside of a struct scope
No overloaded operators in D can be put outside of a struct or class. They have to be member functions. - Jonathan M Davis
Aug 10 2014
parent reply "Era Scarecrow" <rtcvb32 yahoo.com> writes:
On Sunday, 10 August 2014 at 18:58:50 UTC, Jonathan M Davis wrote:
 No overloaded operators in D can be put outside of a struct or 
 class. They have to be member functions.
If I remember right, opApply was somewhat broken and only worked correctly in a few cases. But that was 18 months ago, a lot could have happened...
Aug 10 2014
parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Sunday, 10 August 2014 at 19:01:18 UTC, Era Scarecrow wrote:
 On Sunday, 10 August 2014 at 18:58:50 UTC, Jonathan M Davis 
 wrote:
 No overloaded operators in D can be put outside of a struct or 
 class. They have to be member functions.
If I remember right, opApply was somewhat broken and only worked correctly in a few cases. But that was 18 months ago, a lot could have happened...
I'm not aware of opApply being broken, but I never use it, since in most cases where you might use opApply, you can use ranges, and they're far more flexible. But regardless, it's not legal to declare an overloaded operator outside of the type that it's for, so whether you're talking about opApply, opBinary, opAssign, or any other overloaded operator, declaring it as a free function like the OP is trying to do isn't going to work. - Jonathan M Davis
Aug 10 2014
parent reply "Era Scarecrow" <rtcvb32 yahoo.com> writes:
On Sunday, 10 August 2014 at 21:57:29 UTC, Jonathan M Davis wrote:
 I'm not aware of opApply being broken, but I never use it,
I remember very specifically it was brought up, that opApply was not working correctly and you could only use it with a very specific cases because... the implementation was incomplete? I think the discussion was partially on removing opApply from BitArray and going for a range or array approach instead because the problem would go away, but a lot of this is fuzzy and from memory. I have so much to catch up on :(
Aug 10 2014
next sibling parent "Era Scarecrow" <rtcvb32 yahoo.com> writes:
On Sunday, 10 August 2014 at 22:03:28 UTC, Era Scarecrow wrote:
  I remember very specifically it was brought up,
On Wed, Jul 09, 2014 at 03:16:37PM -0700, H. S. Teoh via Digitalmars-d wrote:
 Judging from this, a big missing piece of the current 
 implementation is the actual enforcement of 'scope'.
Or it was that scope was originally suppose to be part of the opApply and that's what i'm thinking about.... stuff i find in other threads...
Aug 10 2014
prev sibling parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Sunday, 10 August 2014 at 22:03:28 UTC, Era Scarecrow wrote:
 On Sunday, 10 August 2014 at 21:57:29 UTC, Jonathan M Davis 
 wrote:
 I'm not aware of opApply being broken, but I never use it,
I remember very specifically it was brought up, that opApply was not working correctly and you could only use it with a very specific cases because... the implementation was incomplete? I think the discussion was partially on removing opApply from BitArray and going for a range or array approach instead because the problem would go away, but a lot of this is fuzzy and from memory. I have so much to catch up on :(
IIRC, opApply doesn't play well with various attributes, but I don't remember the details. That's the only issue with opApply that I'm aware of. It looks like you'll have to go digging into those other threads if you want to know what was supposed to be wrong with it. - Jonathan M Davis
Aug 10 2014
parent "Era Scarecrow" <rtcvb32 yahoo.com> writes:
On Monday, 11 August 2014 at 02:03:15 UTC, Jonathan M Davis wrote:
 IIRC, opApply doesn't play well with various attributes, but I 
 don't remember the details. That's the only issue with opApply 
 that I'm aware of. It looks like you'll have to go digging into 
 those other threads if you want to know what was supposed to be 
 wrong with it.
Pretty sure it was the scope keyword rather than opApply. Maybe that it didn't play well with out or inout or some combination, or const or something... http://forum.dlang.org/thread/mailman.1363.1339173331.24740.digitalmars-d-learn puremagic.com Oh well. Not important enough to keep talking about.
Aug 10 2014