Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Suggestion: Make all structs immutable  (Read 12577 times)

0 Members and 1 Guest are viewing this topic.

krzat

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Suggestion: Make all structs immutable
« Reply #15 on: July 12, 2014, 10:21:26 pm »
If you really want, you can write constructor like this:
public Vector2f(float x = default(float), float y = default(float))
{
...
}
 
SFML.Utils - useful extensions for SFML.Net

Joki

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Suggestion: Make all structs immutable
« Reply #16 on: July 13, 2014, 12:45:06 pm »
Quote
Because for you to use this syntax there is 2 requirements: what you are assigning is a property (not a plain old variable), and that the property can be assigned

This is plain wrong.

This works,
Code: [Select]
RenderStates r = new RenderStates { BlendMode = BlendMode.Add, Texture = new Texture(@"blah.png") };

while these being fields, NOT properties:

Code: [Select]
/// <summary>Blending mode</summary>
public BlendMode BlendMode;

/// <summary>Transform</summary>
public Transform Transform;

/// <summary>Texture</summary>
public Texture Texture;

/// <summary>Shader</summary>
public Shader Shader;

Quote
(and it won't if the struct is immutable as the property can't have a setter).

Of course it can.  :D

Code: [Select]
struct Foo
{
public Foo(int b)
{
boo = b;
}

private readonly int boo;
public int Boo
{
get { return boo; }
set { Console.WriteLine("I exist."); }
}
}

krzat

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Suggestion: Make all structs immutable
« Reply #17 on: July 13, 2014, 02:24:13 pm »
It won't work for immutable structs.
SFML.Utils - useful extensions for SFML.Net

Joki

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Suggestion: Make all structs immutable
« Reply #18 on: July 13, 2014, 03:04:51 pm »
That's not what i said.

krzat

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Suggestion: Make all structs immutable
« Reply #19 on: July 13, 2014, 03:33:18 pm »
You kinda did before. Initializer syntax is not very useful for immutable struct, but you mentioned it.
SFML.Utils - useful extensions for SFML.Net

Joki

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Suggestion: Make all structs immutable
« Reply #20 on: July 14, 2014, 07:13:49 pm »
Hm sorry, i didn't clarify it well.

Can initialization syntax be used to assign to something, that is not a property, but a regular field?
Yes, it can.

Is it usefull for immutable structs?
Sadly no. :(

You could still use reflection (either using Activator which is really slow, or compiled ExpressionTrees which is overkill) to assign to private fields without creating every possible constructor.  :-\