3 ms·
> Anyway, getters and setters are an antipattern as well since one can just as well make all the fields public. I Java? Yes. Because of the language design, an
by troupo 10d ago
> Anyway, getters and setters are an antipattern as well since one can just as well make all the fields public.
I Java? Yes. Because of the language design, and not because of some inherent "encapsulation" or something.
Somehow `x with { a = b }` doesn't break encapsulation and offers nice DX. But object initializers? Lol
> And a class will be able to choose which things can be set, which is not the case for initializers.
The class in C# can easily chose what can and cannot be set. Because unlike Java it actually cares about things like this.
Quick example
class Test {
public int x { get; set; }
public int y { get; }
}
var t = new Test{ x = 1, y = 2 };
I can give you a hint: this will not compile.
Another example:
public class Matrix
{
private double[,] storage = new double[3, 3];
public double this[int row, int column]
{
// The embedded array will throw out of range exceptions as appropriate.
get { return storage[row, column]; }
set { storage[row, column] = value; }
}
}
var identity = new Matrix
{
[0, 0] = 1.0,
[0, 1] = 0.0,
[0, 2] = 0.0,
[1, 0] = 0.0,
[1, 1] = 1.0,
[1, 2] = 0.0,
[2, 0] = 0.0,
[2, 1] = 0.0,
[2, 2] = 1.0,
};
Incomprehensible abilities for Java-land
> That code won't look that much different with async/await.
Lol. https://news.ycombinator.com/item?id=49726868 https://news.ycombinator.com/item?id=49726868
- samus 8d agoYou can go with a lot less code if you don't care about proper cleanup of resources.