Live data from Hacker News

What's New in C# 6.0 [video]

channel9.msdn.com

141–145 of 145 posts

Re: What's New in C# 6.0 [video]

#141
post #140
post #134

Still no first-class tuple support? Argh! All want is: var first, last = parseName(fullName); Instead of: String last; var first = parseName(fullName, out last);

Totally agree on the need for tuple syntax (and pattern matching to make it really useful): return Tuple.Create(foo,bar); Should be: return (foo,bar); The worst part is using the Item1..ItemN properties. One way I've gotten around it is to create a Tuple extension method called Apply [1]. I allows you to do this: ParseName(fullName).Apply((first, last) => ... ) That allows for giving names to the tuple ItemX fields a…

Apply creates new scope, which is often undesirable. Consider the "Declaration expressions" feature in C# 6:

  ParseName(fullName).Unpack(out string first, out string last);
However, this makes for essentially left-to-right assignment, which is contrary to the usual order. So this creates discontinuity when moving from one return value to two return values.

Re: What's New in C# 6.0 [video]

#142
post #111

So these lines are now equivalent: public override string ToString() { return String.Format("({0}, {1})", X, Y); } public override string ToString() => "(\{X}, \{Y})" Could it go even further? This isn't paper after all, on screen why shouldn't it look like; public override string ToString() => "(X, Y)" where the X and Y are either slight italics or underlined or colored to indicate they mean X and Y the variable not…

>where the X and Y are either slight italics or underlined or colored to indicate they mean X and Y the variable not X and Y the letters So what happens if you open the file in a different editor?

I guess it could show up with the \{X} escape notation in Notepad. But in theory almost no one would even notice that, the "natural form" would be to eliminate excessive escaping and just show what the string will actually be.

Re: What's New in C# 6.0 [video]

#143
post #141
post #140

Earlier quoted context omitted.

Totally agree on the need for tuple syntax (and pattern matching to make it really useful): return Tuple.Create(foo,bar); Should be: return (foo,bar); The worst part is using the Item1..ItemN properties. One way I've gotten around it is to create a Tuple extension method called Apply [1]. I allows you to do this: ParseName(fullName).Apply((first, last) => ... ) That allows for giving names to the tuple ItemX fields a…

Apply creates new scope, which is often undesirable. Consider the "Declaration expressions" feature in C# 6: ParseName(fullName).Unpack(out string first, out string last); However, this makes for essentially left-to-right assignment, which is contrary to the usual order. So this creates discontinuity when moving from one return value to two return values.

My personal opinion is 'out' is a total abomination. I will do everything I can to avoid using it. I prefer expression based programming where there's a balance between the two sides of an operator. Declaring named values on the RHS is just horrible (again, IMHO). The only reason for it to exist is because C# hasn't got proper tuple support.

By the way, creating a new variable half-way down a method is also creating a new scope (the variable isn't in scope above the declaration, and it is in scope below it, until the end of the method).

For example:

    public int GetSurnameLength()
    {
        string fullName = ReadFullNameFromDb();
        Tuple result = ParseName(fullName);
        return result.Item2.Length;
    }  
Has the same effect as:

    public int GetSurnameLength()
    {
        string fullName = ReadFullNameFromDb();
        return ParseName(fullName).Apply( (first,last) => last.Length );
    }  

The only real downside to the Apply method is the syntax clutter from the closure. But there's no real issue with scope as far as I can tell (because the rest of the method can be within the closure if necessary). This is very similar to how 'let' works in F#.

Re: What's New in C# 6.0 [video]

#144
post #111

So these lines are now equivalent: public override string ToString() { return String.Format("({0}, {1})", X, Y); } public override string ToString() => "(\{X}, \{Y})" Could it go even further? This isn't paper after all, on screen why shouldn't it look like; public override string ToString() => "(X, Y)" where the X and Y are either slight italics or underlined or colored to indicate they mean X and Y the variable not…

> where the X and Y are either slight italics or underlined or colored to indicate they mean X and Y the variable not X and Y the letters. The IDE would let you just toggle the state between 'variable' and 'literal' with the keyboard. A big nope from me. Nicer visual representation is OK. Changing syntax based on visual attributes it's not.

[Edit: this perhaps should have been "Changing semantics based on visual attributes is not"].

Re: What's New in C# 6.0 [video]

#145
post #128

Earlier quoted context omitted.

> IsNullOrWhiteSpace() was added in dot net 4: Look at what the helper actually does. I already addressed this in my comment. string hello = magic(); if(hello.IsNullOrWhiteSpace()) { ... } Is significantly more intuitive than: string hello = magic(); if(string.IsNullOrWhiteSpace(hello)) { ... } Plus it is consistent with things like Contains("XYZ") (e.g. hello.Contains("something")). Truncate is useful just because S…

That defeats half the purpose though, because if the string is null, you won't be able to call the method without throwing an exception. Or is that not the case for extension methods?

Extension methods are rewritten by the compiler to a normal static method call with the subject as their first argument (that's how they're defined, anyway). And since you can legitimately pass null to those methods there will be no NRE by default.
Post reply on HN