Giter Club home page Giter Club logo

Comments (5)

DavidArno avatar DavidArno commented on June 16, 2024 1

Well yes, I do. And having gone full circle, I'm re-opening this.

The reason I claimed that readonly structs are weird is due to the distinction between "read only" and "immutable". In C#, readonly denotes that the value of that item cannot change. This means that I might have the class,

class C
{
    readonly List<int> _x = new List<int>();

    void ReadonlyNotImmutable() => _x.Add(1);
}

The "value" of _x can't change, ie it is fixed to referring to the same List<int> object. But the contents can change, ie it's readonly not immutable.

Readonly structs apply this same rule, but it creates odd results. So for example, the following code:

class FakeTuple
{
    public int a;
    public int b;
}

readonly struct S
{
    readonly (int a, int b) _x;
    readonly FakeTuple _y;
    
    public S(int n)
    {
        _x = (n, n);
        _y = new FakeTuple { a = n, b = n };
    }
    
    int X { set { _x.a = value; } }           
    int Y { set { _y.a = value; }}
}

gives an error on _x.a = value; but not on _y.a = value;. Further, despite marking it as readonly, I can still have setters. So I really struggled with the idea that it's readonly as to me, that ought to rule out setters.

But what I was missing is the simple idea that, just like with a readonly field, a readonly struct guarantees its value cannot change. And the value of a struct is the value of its fields. So the tuple, being a struct itself, cannot change the value of its fields as that would change its own value and thus the value of the struct, S. Likewise the reference for the FakeTuple field can't change, but items it contains can, as they aren't part of the struct's value. And setters are allowed, as long as they don't change the value of the struct.

So having had that lightbulb moment, I've gone back and re-introduced that readonly struct for Option. 😄

from succinct.

DavidArno avatar DavidArno commented on June 16, 2024

Have abandoned this idea as read only structs are just weird.

from succinct.

SugoiDev avatar SugoiDev commented on June 16, 2024

Do you have any insight on the weirdness you found?
I was planning some changes on a codebase using readonly structs soon.

from succinct.

DavidArno avatar DavidArno commented on June 16, 2024

Adding a checklist here for all the types that can become readonly structs:

  • Any
  • Either
  • EitherTuple<T1, T2> (internal type)
  • EitherTuple<T1, T2, T3> (internal type)
  • EitherTuple<T1, T2, T3, T4> (internal type)
  • None
  • Option<T>
  • Success<T>
  • ValueOrError
  • ValueOrError<TV, TE>
  • Union<T1, T2>
  • Union<T1, T2, T3>
  • Union<T1, T2, T3, T4>
  • Unit

from succinct.

DavidArno avatar DavidArno commented on June 16, 2024

Maybe<T> is gone and Option<T> is now a read-only struct.

Closing as implemented.

from succinct.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.