November 25, 2005 Re: ctor for stucts? | ||||
---|---|---|---|---|
| ||||
Posted in reply to kris | kris wrote: <snip> >> What would happen if you then try to use such a struct as a global variable or a member of a class or struct? >> >> Stewart. > > D doesn't currently support assignment at declaration for anything but constant data. When that changes, down the road, it would presumably support the condition you note? It allows you to assign anything, constant or not, when declaring a local variable. Just not in other circumstances. Would changing this really be easy to implement, and can it be done without leading to confusing or undefined behaviour when initialisers may depend on each other? > Alternatively, you might be indicating that the construction should be delayed? If so, then don't put a ctor in the struct. It would operate in the same two phase (init + setup) approach as it does today? So (for now at least) if you put a constructor in a struct, it'll become illegal to declare one except at function level? Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- C++@ a->--- UB@ P+ L E@ W++@ N+++ o K-@ w++@ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit. |
November 25, 2005 Re: ctor for stucts? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | Stewart Gordon wrote:
> kris wrote:
> <snip>
>
>>> What would happen if you then try to use such a struct as a global variable or a member of a class or struct?
>>>
>>> Stewart.
>>
>>
>> D doesn't currently support assignment at declaration for anything but constant data. When that changes, down the road, it would presumably support the condition you note?
>
>
> It allows you to assign anything, constant or not, when declaring a local variable. Just not in other circumstances. Would changing this really be easy to implement, and can it be done without leading to confusing or undefined behaviour when initialisers may depend on each other?
>
>> Alternatively, you might be indicating that the construction should be delayed? If so, then don't put a ctor in the struct. It would operate in the same two phase (init + setup) approach as it does today?
>
>
> So (for now at least) if you put a constructor in a struct, it'll become illegal to declare one except at function level?
You might not be able to use the suggested ctor at, for example, global scope, if you don't already have the arguments to pass to it. Conceptually, the syntax might be something like this:
struct Foo
{
this (int a, char b) {}
}
Foo foo(1, 'd');
From purely a dependency standpoint, this is no different that class construction.
Do you have another approach? The problem is ensuring the struct is correctly initialised, and only when that is needed, using a convenient syntax. The invarient thing, whilst indeed useful, does not really address the latter.
==============
To illustrate the current situation:
struct Foo
{
void ctor(int a, char b) {}
}
Foo foo;
foo.ctor (1, 'd');
|
Copyright © 1999-2021 by the D Language Foundation