www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - anonymus struct or class instances as arguments

reply Leopold Walkling <leopold_walkling web.de> writes:
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
next sibling parent reply Karen Lanrap <karen digitaldaemon.com> writes:
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;

{ A retval; retval.i = p; return retval; }
 }

 void funk(A);
 
 int main() {

      return 0;
 }

Dec 07 2006
parent Leopold Walkling <leopold_walkling web.de> writes:
Karen Lanrap schrieb:
 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;

{ A retval; retval.i = p; return retval; }
 }

 void funk(A);

 int main() {

      return 0;
 }


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 operations
Dec 07 2006
prev sibling next sibling parent reply Lionello Lunesu <lio lunesu.remove.com> writes:
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
parent reply Karen Lanrap <karen digitaldaemon.com> writes:
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
parent Leopold Walkling <leopold_walkling web.de> writes:
Yes that'Karen Lanrap schrieb:
 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

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.
Dec 08 2006
prev sibling parent Leopold Walkling <leopold_walkling web.de> writes:
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