No, no, and no again.
This will break lots of existing code and cause more issues than the two we already have.
Issue #1
// default constructorRenderStates states
= new RenderStates
();
Issue #2 (not really an issue as any C# dev should understand this)
// compiler error, trying to modify temporary object
vertexarray[0].Color = Color.Red;
With mutable structs it is extremely easy to loss data, when e.g. users accidently modify a copy of a struct instead of the structs
This isn't the case anywhere with the SFML.NET bindings, not to mention any decent C# dev will know that modifying value types that have been copied into methods won't change it outside of the method. Or a quick google search will bring this up.
Immutable structs are automatically thread safe by nature
Doesn't apply to SFML as we aren't trying to be thread safe with the public API.
Users will no longer forget to initialize some of the fields
How so? It doesn't force them to use the non-default constructor and all non-default constructors initialize everything with default values. Making structs immutable will require us to now provide every single possible constructor since the user now can't change values after constructing an object. Just think of vectors, to change a vector's value you will now need to create a new vector every single time you want to modify it instead of just changing the existing value.
turn the fields into properties, and have the setter return a new instance of the modified struct.
What fields into properties?
I'm at work now, but if this isn't enough to convince you that this isn't right for SFML.NET I will gladly pull out more specific code examples that will break if we make stuff immutable in the binding.
/// <summary>/// Define a point with color and texture coordinates/// </summary>[StructLayout
(LayoutKind
.Sequential)]public struct Vertex
{ /// <summary> /// Construct the vertex from its position /// The vertex color is white and texture coordinates are (0, 0). /// </summary> /// <param name="position">Vertex position</param> public Vertex
(Vector2f position
) : this(position, Color
.White,
new Vector2f
(0,
0)) { } /// <summary> /// Construct the vertex from its position and color /// The texture coordinates are (0, 0). /// </summary> /// <param name="position">Vertex position</param> /// <param name="color">Vertex color</param> public Vertex
(Vector2f position, Color color
) : this(position, color,
new Vector2f
(0,
0)) { } /// <summary> /// Construct the vertex from its position and texture coordinates /// The vertex color is white. /// </summary> /// <param name="position">Vertex position</param> /// <param name="texCoords">Vertex texture coordinates</param> public Vertex
(Vector2f position, Vector2f texCoords
) : this(position, Color
.White, texCoords
) { } /// <summary> /// Construct the vertex from its position, color and texture coordinates /// </summary> /// <param name="position">Vertex position</param> /// <param name="color">Vertex color</param> /// <param name="texCoords">Vertex texture coordinates</param> public Vertex
(Vector2f position, Color color, Vector2f texCoords
) { this.position = position
; this.color = color
; this.texCoords = texCoords
; } /// <summary> /// Provide a string describing the object /// </summary> /// <returns>String description of the object</returns> public override string ToString
() { return "[Vertex]" + " Position(" + Position
+ ")" + " Color(" + Color
+ ")" + " TexCoords(" + TexCoords
+ ")"; } /// <summary>2D position of the vertex</summary> public Vector2f Position
{ get { return Position
; } } private readonly Vector2f position
; /// <summary>Color of the vertex</summary> public Color Color
{ get { return color
; } } private readonly Color color
; /// <summary>Coordinates of the texture's pixel to map to the vertex</summary> public Vector2f TexCoords
{ get { return texCoords
; } } private readonly Vector2f texCoords
;}class Program
{ static void Main
(string[] args
) { // oops, accidently called default ctor on immutable struct Vertex badVertex
= new Vertex
(); // notice at compile time Console
.WriteLine(badVertex
); badVertex
.Color = Color
.Red; Console
.WriteLine(badVertex
); // hey, thats fine. Vertex vertex
= new Vertex
(new Vector2f
(50,
50), Color
.Black); }}
The advantages were mentioned, as suggested by Eric Lippert, Microsoft, and every serious book on topic. A value type should just be used as a container to represent values, that are not changable in any way.
I am not willing to get into a flame war with you, zsbzsb.
It was a suggestion.
If you don't like it, don't do it.
If you like it, do it.
If you are unsure, use that well-known search engine what the developers of the C# language and CLR's take is on this issue.
If you really want, you can write constructor like this:
public Vector2f(float x = default(float), float y = default(float))
{
...
}