digitalmars.D.learn - overload of array operations
- Jay Norwood (1/1) Oct 14 2011 Is it possible to overload array operations
- Jonathan M Davis (5/6) Oct 14 2011 Please be more specific. Are you asking whether a struct or class can ov...
- Jay Norwood (4/13) Oct 14 2011 I'm interested, for example, in overloading the + operation for arrays...
- Jay Norwood (3/12) Oct 14 2011 to be more specific, I'm interested in overloading the vector operations...
- Jonathan M Davis (8/23) Oct 14 2011 You could probably do it if you're fancy, but there's no explicit way to...
- Jay Norwood (6/32) Oct 14 2011 Yes, I intended to try to overload the array operations on an array of s...
- Jonathan M Davis (8/49) Oct 14 2011 So, you want to have use vector operations on arrays of structs rather t...
- Timon Gehr (2/34) Oct 17 2011 I agree. It is odd that we have opSliceUnary but not opSliceBinary.
- bearophile (4/5) Oct 17 2011 If you think something useful is missing, then file a Bugzilla enhanceme...
- Timon Gehr (9/32) Oct 17 2011 I disagree. Operator overloads are there to make types act like built-in...
On Friday, October 14, 2011 11:30:25 Jay Norwood wrote:Is it possible to overload array operationsPlease be more specific. Are you asking whether a struct or class can overload the indexing and slicing operators? If so, the answer is yes. http://d-programming-language.org/operatoroverloading.html - Jonathan M Davis
Oct 14 2011
Jonathan M Davis Wrote:On Friday, October 14, 2011 11:30:25 Jay Norwood wrote:I'm interested, for example, in overloading the + operation for arrays that is documented to generate a vector add operation. I assume there would need to be a name for that operation in order to overload it. Thanks, JayIs it possible to overload array operationsPlease be more specific. Are you asking whether a struct or class can overload the indexing and slicing operators? If so, the answer is yes. http://d-programming-language.org/operatoroverloading.html - Jonathan M Davis
Oct 14 2011
Jonathan M Davis Wrote:On Friday, October 14, 2011 11:30:25 Jay Norwood wrote:to be more specific, I'm interested in overloading the vector operations on arrays described at this link, search for "vector operation" http://www.digitalmars.com/d/2.0/arrays.htmlIs it possible to overload array operationsPlease be more specific. Are you asking whether a struct or class can overload the indexing and slicing operators? If so, the answer is yes. http://d-programming-language.org/operatoroverloading.html - Jonathan M Davis
Oct 14 2011
On Friday, October 14, 2011 15:29:17 Jay Norwood wrote:Jonathan M Davis Wrote:You could probably do it if you're fancy, but there's no explicit way to have a struct or class operate like that. If you really wanted to though, you could overload opSlice on your struct to return a specific type which then overloaded opBinary for + and then have that return a new struct with the changed values. But that borders on overloaded operator abuse. Vector operations are really only intended for arrays. - Jonathan M DavisOn Friday, October 14, 2011 11:30:25 Jay Norwood wrote:to be more specific, I'm interested in overloading the vector operations on arrays described at this link, search for "vector operation" http://www.digitalmars.com/d/2.0/arrays.htmlIs it possible to overload array operationsPlease be more specific. Are you asking whether a struct or class can overload the indexing and slicing operators? If so, the answer is yes. http://d-programming-language.org/operatoroverloading.html - Jonathan M Davis
Oct 14 2011
Jonathan M Davis Wrote:On Friday, October 14, 2011 15:29:17 Jay Norwood wrote:Yes, I intended to try to overload the array operations on an array of structures. I just want to use the simple syntax for the operations. a[]= b[]+c[]; a[] = b[] + 4; a[] *= 4; where a, b, c are arrays of structures, and then overload the operations. It seems to me that should require names for the array operations being overloaded.Jonathan M Davis Wrote:You could probably do it if you're fancy, but there's no explicit way to have a struct or class operate like that. If you really wanted to though, you could overload opSlice on your struct to return a specific type which then overloaded opBinary for + and then have that return a new struct with the changed values. But that borders on overloaded operator abuse. Vector operations are really only intended for arrays. - Jonathan M DavisOn Friday, October 14, 2011 11:30:25 Jay Norwood wrote:to be more specific, I'm interested in overloading the vector operations on arrays described at this link, search for "vector operation" http://www.digitalmars.com/d/2.0/arrays.htmlIs it possible to overload array operationsPlease be more specific. Are you asking whether a struct or class can overload the indexing and slicing operators? If so, the answer is yes. http://d-programming-language.org/operatoroverloading.html - Jonathan M Davis
Oct 14 2011
On Friday, October 14, 2011 16:12 Jay Norwood wrote:Jonathan M Davis Wrote:So, you want to have use vector operations on arrays of structs rather than use vector operations on a struct or class? If that's what you want, then overload the arithmetic operations on the struct as normal, and then use vector operations on the array holding the structs. It may work. I don't know. If it doesn't, then you're out of luck and can't use the built in syntax. And if does, well then there you go. - Jonathan M DavisOn Friday, October 14, 2011 15:29:17 Jay Norwood wrote:Yes, I intended to try to overload the array operations on an array of structures. I just want to use the simple syntax for the operations. a[]= b[]+c[]; a[] = b[] + 4; a[] *= 4; where a, b, c are arrays of structures, and then overload the operations. It seems to me that should require names for the array operations being overloaded.Jonathan M Davis Wrote:You could probably do it if you're fancy, but there's no explicit way to have a struct or class operate like that. If you really wanted to though, you could overload opSlice on your struct to return a specific type which then overloaded opBinary for + and then have that return a new struct with the changed values. But that borders on overloaded operator abuse. Vector operations are really only intended for arrays. - Jonathan M DavisOn Friday, October 14, 2011 11:30:25 Jay Norwood wrote:to be more specific, I'm interested in overloading the vector operations on arrays described at this link, search for "vector operation" http://www.digitalmars.com/d/2.0/arrays.htmlIs it possible to overload array operationsPlease be more specific. Are you asking whether a struct or class can overload the indexing and slicing operators? If so, the answer is yes. http://d-programming-language.org/operatoroverloading.html - Jonathan M Davis
Oct 14 2011
On 10/15/2011 01:12 AM, Jay Norwood wrote:Jonathan M Davis Wrote:I agree. It is odd that we have opSliceUnary but not opSliceBinary.On Friday, October 14, 2011 15:29:17 Jay Norwood wrote:Yes, I intended to try to overload the array operations on an array of structures. I just want to use the simple syntax for the operations. a[]= b[]+c[]; a[] = b[] + 4; a[] *= 4; where a, b, c are arrays of structures, and then overload the operations. It seems to me that should require names for the array operations being overloaded.Jonathan M Davis Wrote:You could probably do it if you're fancy, but there's no explicit way to have a struct or class operate like that. If you really wanted to though, you could overload opSlice on your struct to return a specific type which then overloaded opBinary for + and then have that return a new struct with the changed values. But that borders on overloaded operator abuse. Vector operations are really only intended for arrays. - Jonathan M DavisOn Friday, October 14, 2011 11:30:25 Jay Norwood wrote:to be more specific, I'm interested in overloading the vector operations on arrays described at this link, search for "vector operation" http://www.digitalmars.com/d/2.0/arrays.htmlIs it possible to overload array operationsPlease be more specific. Are you asking whether a struct or class can overload the indexing and slicing operators? If so, the answer is yes. http://d-programming-language.org/operatoroverloading.html - Jonathan M Davis
Oct 17 2011
Timon Gehr:I agree. It is odd that we have opSliceUnary but not opSliceBinary.If you think something useful is missing, then file a Bugzilla enhancement request. Bye, bearophile
Oct 17 2011
On 10/14/2011 09:43 PM, Jonathan M Davis wrote:On Friday, October 14, 2011 15:29:17 Jay Norwood wrote:I disagree. Operator overloads are there to make types act like built-in types, which is what he wants. Overloaded operator abuse is the act of creating a type that uses the operators for different semantics than the built-in ones. What you describe is actually close to how the language handles dynamic array slices. But reproducing the behavior with operator overloading does indeed not work nicely because of type inference. (we'd need a way to overload the typeof operator ;))Jonathan M Davis Wrote:You could probably do it if you're fancy, but there's no explicit way to have a struct or class operate like that. If you really wanted to though, you could overload opSlice on your struct to return a specific type which then overloaded opBinary for + and then have that return a new struct with the changed values. But that borders on overloaded operator abuse. Vector operations are really only intended for arrays. - Jonathan M DavisOn Friday, October 14, 2011 11:30:25 Jay Norwood wrote:to be more specific, I'm interested in overloading the vector operations on arrays described at this link, search for "vector operation" http://www.digitalmars.com/d/2.0/arrays.htmlIs it possible to overload array operationsPlease be more specific. Are you asking whether a struct or class can overload the indexing and slicing operators? If so, the answer is yes. http://d-programming-language.org/operatoroverloading.html - Jonathan M Davis
Oct 17 2011