G
Guest
I would like to define a structure or a class with an array field that
behaves like a simple value-semantics variable. For example, I want
something like
public structure polynomial
public a() as double ' the polynomial coefficients
...
end structure
that will behave like
public structure complex
public x,y as double ' the real and imaginary parts
...
end structure
If I declare u and v as complex, no operation on u will affect v. In other
words, the complex structure has value semantics.
If I declare u and v as polynomial, initialize v, and then assign v to u (ie
execute the statement u=v), I now have two references to the same array. An
update to u.a is simultaneously an update to v.a because both u and v have
references to the same array. In other words, polynomial does not have value
semantics.
My question is this - Is there a way to achieve value semantics with an array?
Perhaps the proper word to use here is immutability, but I don't want to get
hung up on these definitions. If I declare variables of type double, I get
value semantics. If I make a structure that contains only doubles, I also
get value semantics. But if I make an array of doubles or a class/structure
that contains an array, I do not get value semantics because an array is a
reference type. What I want is value type treatment of an array.
I am fully aware of the performance issues here. Please no performance
digressions. What I want to know is feasiblility - can it be done? I have
searched and it appears that it can't be done.
FYI, the purpose of this question is just what the examples imply, namely
some mathematical functionality that involves complex numbers, polynomials,
vectors and other algebraic elements with operator overloads and other
extensions. I want value semantics throughout - I want to program with
variables of these new types in the same way I program with a double. But I
also need arrays to get the job done.
behaves like a simple value-semantics variable. For example, I want
something like
public structure polynomial
public a() as double ' the polynomial coefficients
...
end structure
that will behave like
public structure complex
public x,y as double ' the real and imaginary parts
...
end structure
If I declare u and v as complex, no operation on u will affect v. In other
words, the complex structure has value semantics.
If I declare u and v as polynomial, initialize v, and then assign v to u (ie
execute the statement u=v), I now have two references to the same array. An
update to u.a is simultaneously an update to v.a because both u and v have
references to the same array. In other words, polynomial does not have value
semantics.
My question is this - Is there a way to achieve value semantics with an array?
Perhaps the proper word to use here is immutability, but I don't want to get
hung up on these definitions. If I declare variables of type double, I get
value semantics. If I make a structure that contains only doubles, I also
get value semantics. But if I make an array of doubles or a class/structure
that contains an array, I do not get value semantics because an array is a
reference type. What I want is value type treatment of an array.
I am fully aware of the performance issues here. Please no performance
digressions. What I want to know is feasiblility - can it be done? I have
searched and it appears that it can't be done.
FYI, the purpose of this question is just what the examples imply, namely
some mathematical functionality that involves complex numbers, polynomials,
vectors and other algebraic elements with operator overloads and other
extensions. I want value semantics throughout - I want to program with
variables of these new types in the same way I program with a double. But I
also need arrays to get the job done.