Feature request: Deploying a class instance in the default data segment
resume755 at mail.ru
Sun Dec 21 00:19:01 PST 2008
Bill Baxter пишет:
> On Thu, Dec 18, 2008 at 2:05 PM, Weed <resume755 at mail.ru> wrote:
>> Feature request: Deploying a class instance in the default data segment and
>> creation of a class instance at compile time.
>> In this post I'll try to prove the necessity of this feature.
>> I have found that in D it is impossible to create a polymorphic object
>> (class or struct) which can be created at compile-time.
>> Good specific example of such an object is a matrix class.
>> For some matrices it is good to create and initialize them at compile
>> time, not dynamically at run time.
>> Also for matrices of predefined size it is wrong to duplicate their
>> dimensions (their width and height should be declared invariant and,
>> consequently occupy memory only once)
>> At the same time dynamically created matrices of arbitrary size which
>> can be changed at run time are also needed. For example to make a slice of
>> giant matrix it's more efficient to modify it in-place than to copy it.
>> These matrices have variable width and height.
>> So we have minimum two types of matrices: dynamic and constant sized with
>> ability of compile time initialization. It is necessary to provide
>> interaction between them (for example overload arithmetic operators
>> for them). For this purpose it is necessary for them to be inherited
>> from common parent.
> This point I don't think is true. DynamicMatrix can implement methods
> like opMult(StaticMatrix M), and vice versa.
> The only thing I can think of you can't do is make an array of
> pointers to Matrix, the elements of which might be a DynamicMatrix, or
> might be StaticMatrix.
> But even that, if you really feel you need it for some reason, could
> be done using a struct/class with a union inside. Most cases where
> you might want to accept either/or can be done with templates.
>> So it can not be structures, only classes.
>> But classes can not be created at compile time.
>> So I propose to add possibility of deployment of class instance in the
>> default data segment and creation of class instance at compile time.
> This might be a useful feature, but I'm not finding your argument for
> it quite convincing enough yet.
Here other example:
Suppose a matrix structure implemented "Dynamic" and "Static" using the
On this basis must be built other structures (for example, the structure
of "image" is a dynamic matrix with additional features, the structure
of "pixel" is a matrix of 3x1).
Functional of matrix structures "Static" and "Dynamic" included in the
structure of "image" and "pixel". So far, so good.
But in this case becomes impossible to implement matrix multiplication
operation type, return other than the size of the source matrix, just as
template "template MultReturnType (ArgT)" - we will not be able to
create a matrix of different dimensions but the same type as the
original matrix, because we will have to list in this template
"MultReturnType" all structures "image", "pixel", etc.
If the matrix would be a class that would be to return the reference to
base type matrix enough
More information about the Digitalmars-d