Comments (5)
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.
Have abandoned this idea as read only structs are just weird.
from succinct.
Do you have any insight on the weirdness you found?
I was planning some changes on a codebase using readonly structs soon.
from succinct.
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.
Maybe<T>
is gone and Option<T>
is now a read-only struct.
Closing as implemented.
from succinct.
Related Issues (20)
- Generic ValueOrError HOT 3
- A non-exception Value<T> alternative for unions HOT 2
- Maybe.Some(null) does not throw and has a different behavior than Maybe.None() HOT 8
- SuccincT Roadmap HOT 12
- When will version 3.1.1 be published to NuGet? HOT 3
- Checklist for the v4 release HOT 1
- As C# 7 now supports discards, the Ignore function is redundant and should be removed HOT 1
- As C# 7 now supports discards, the Ignore function is redundant and should be removed HOT 1
- Add pattern matching to the Either type HOT 1
- Runtime Exception of type System.IO.FileNotFoundException HOT 4
- Change enum parsers to use Enum generic constraint HOT 1
- Add positional pattern matching support to Option<T> HOT 1
- Add positional pattern matching support to ValueOrError types HOT 1
- Add positional pattern matching support to Success<T> HOT 1
- Add positional pattern matching support to Either<TL,TR> HOT 1
- Add positional pattern matching support to Union types HOT 1
- Investigate adding cons support for async streams
- Remove the use of <T> at the end of Succinc<T>
- Create a new release of SuccincT, targeting .NET 6, with all the redundant features removed HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from succinct.