digitalmars.D - anonymus struct or class instances as arguments
- Leopold Walkling (25/25) Dec 07 2006 Couldn't there be a syntax for creating a struct or class instance, that...
- Karen Lanrap (9/19) Dec 07 2006 static A opCall(int p)
- Leopold Walkling (7/30) Dec 07 2006 Yes, I use them too, and they really do a good job. But I'm bothering
- Lionello Lunesu (4/4) Dec 08 2006 Sounds like a job for tuples!
- Karen Lanrap (6/7) Dec 08 2006 Those have to be programmed also ...
- Leopold Walkling (6/18) Dec 08 2006 Yes that's nearly what I'm thinking of, it already exists for static
- Leopold Walkling (7/38) Dec 09 2006 I just want to know how the creator imagined the language to be like in
Couldn't there be a syntax for creating a struct or class instance, that only exists for one function? In the moment there are scope classes, but they can't be used without an identifier as an argument. Normal class instances can be given as an argument, but then the destructor can't be called manually, nor is it called automatically before the end of the programm. Why can't the 'scope' keyword be used like this: function(scope new A() // Here the constructor is called); //Here the destructor is called Also there isn't a way to create a struct, only by giving its values. This can be done with something like this, although it's very ugly: struct A { int i; } void funk(A); int main() { funk(*(cast(A*)[cast(int)9].ptr); return 0; } Also this would lead to a slight overhead, because of the cast. Is there any reason why those possibilites don't exist? And what should I use instead of those, if they are refused? My main problem is, that I don't want to create a variable with a name everytime I have to use one Object, that I don't need later, and then call the destructor.
Dec 07 2006
Leopold Walkling wrote:Also there isn't a way to create a struct, only by giving its values.I use it along this lines:struct A { int i;static A opCall(int p) { A retval; retval.i = p; return retval; }}void funk(A); int main() {funk= A(9);return 0; }
Dec 07 2006
Karen Lanrap schrieb:Leopold Walkling wrote:Yes, I use them too, and they really do a good job. But I'm bothering about the performance because first you can't be sure that they are inlined, second you have to implement them and for example if you use a struct you didn't create that doesn't have this, what can you do. Also I wonder what is behind this opCall, how far they're optimized and inlined. I suppose that they create additional operationsAlso there isn't a way to create a struct, only by giving its values.I use it along this lines:struct A { int i;static A opCall(int p) { A retval; retval.i = p; return retval; }}void funk(A); int main() {funk= A(9);return 0; }
Dec 07 2006
Sounds like a job for tuples! I have no idea how to use them, but check the examples on this newsgroups and on the digital mars site. L.
Dec 08 2006
Lionello Lunesu wrote:Sounds like a job for tuples!Those have to be programmed also ... And one is loosing the abstraction of an initialization. Basically the OP seems to wish something like: funk({9}); i.e. struct literals with type deduction.
Dec 08 2006
Yes that'Karen Lanrap schrieb:Lionello Lunesu wrote:Yes that's nearly what I'm thinking of, it already exists for static structs, but only with constants, and also you have to declare a variable first. The solution with tuples wouldn't give me the possibility to use values in runtime.Sounds like a job for tuples!Those have to be programmed also ... And one is loosing the abstraction of an initialization. Basically the OP seems to wish something like: funk({9}); i.e. struct literals with type deduction
Dec 08 2006
Leopold Walkling schrieb:Couldn't there be a syntax for creating a struct or class instance, that only exists for one function? In the moment there are scope classes, but they can't be used without an identifier as an argument. Normal class instances can be given as an argument, but then the destructor can't be called manually, nor is it called automatically before the end of the programm. Why can't the 'scope' keyword be used like this: function(scope new A() // Here the constructor is called); //Here the destructor is called Also there isn't a way to create a struct, only by giving its values. This can be done with something like this, although it's very ugly: struct A { int i; } void funk(A); int main() { funk(*(cast(A*)[cast(int)9].ptr); return 0; } Also this would lead to a slight overhead, because of the cast. Is there any reason why those possibilites don't exist? And what should I use instead of those, if they are refused? My main problem is, that I don't want to create a variable with a name everytime I have to use one Object, that I don't need later, and then call the destructor.I just want to know how the creator imagined the language to be like in this point. Should a programmer create a struct and assign each member a value manually? Or are they supposed to overload the opCall() ? I read some posts about readability and 'simple operations should be simple', but an opCall seems simple though it isn't always. And assignments to each member only make the code larger.
Dec 09 2006