Hacker News new | ask | show | jobs
by tdestan 3385 days ago
Did you read far enough to see the tuple return value syntax?

  var (first, middle, last) = LookupName(id1);
Seems like what you're suggesting, no?
1 comments

Better. But the id1 argument can still be mutated by LookupName in this scope, no?
Depends.

Based on my hazy memory of C#, if id is a struct/value type it is copied, but if it is a class type, there's nothing preventing the method call from mutating it.

Which is unfortunate, I agree.

> here's nothing preventing the method call from mutating it.

There is, the class could be immutable.

    public class Vector
    {
        public readonly int X;
        public readonly int Y;

        public Vector(int x, int y) 
        {
            X = x;
            Y = y;
        }

        public Vector With(int? X = null, int? Y = null) =>
            new Vector(X ?? this.X, Y ?? this.Y);
    }
I believe record-types are proposed for the next release of C#.
Mutable class with immutable struct wrapper would be better on allocations; then pass in the struct wrapper.
This is correct. One of the things I miss coming from C++ is the ability to pass const references. Then you have the advantage of passing a reference instead of copying an object, and the compiler enforces immutability in the function using the reference.

On a similar note, I also really miss the ability to define const class methods (i.e. instance methods which do not modify the instance on which they are invoked). Although, perhaps get-only accessors fill part of that niche in C#.