array with value semantics

  • Thread starter Thread starter Guest
  • Start date Start date
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.
 
AMercer said:
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?
No.

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.

As you can't get value semantics, I believe that immutability is your
best bet. By creating a class and making it immutable, you can copy the
reference of the object without risk. As each instance of the object can
never change, copying the reference and creating a copy of the object
gives the same result.
 
As you can't get value semantics, I believe that immutability is your
best bet. By creating a class and making it immutable, you can copy the
reference of the object without risk. As each instance of the object can
never change, copying the reference and creating a copy of the object
gives the same result.

Thanks Göran, that sounds reasonable. I made a() private and switched to a
class:

public class polynomial
private a() as double ' the polynomial coefficients
...
end class

I added a function to return a clone of a() in case the consumer needs it,
and the operators and methods generally return new poly's. From the
consumer's point of view, I guess there isn't much difference between value
semantics and immutablilty.
 
Back
Top