anonymus struct or class instances as arguments

Leopold Walkling leopold_walkling at web.de
Sat Dec 9 02:28:57 PST 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.



More information about the Digitalmars-d mailing list