News for hackers. Stuff that matters.
Re:New Features in C# 7.0 by ZeroClickOk on Thursday March 09, 2017 @06:51PM (#13835011)
What I love in C# is, each update the language gets even faster, far more natural to write. Soo different of Javascript, where looks you are fighting against the language first, and then if you win, can develop something useful. I use js why I need use it, but I hope use c# instead, high hopes with webassembly, etc
Re:New Features in C# 7.0 by pmarreck on Thursday March 09, 2017 @11:24PM (#13836362)
> Out variables

This is pretty much a terrible language design idea. (Not the "out int x" change, but the whole idea of "out" in an argument list to begin with to indicate values coming out via the arguments.)

The path out of mutable argument bug-hell is not to declare that the values of some argument variables will intentionally get changed by the function in the scope where it is called. It is to make arguments immutable (like they are in the traditional mathy definition of "function"), period, and allow multiple assignment to the function call. What I mean is that, conceptually, ideally, values and state should only flow into the function arguments and only back out of the function call itself.

So for example instead of

> p.GetCoordinates(out int x, out int y);

I think the following would lead to reduced cognitive load and thus fewer bugs:

> int x, int y = p.GetCoordinates;

or even better (color me biased by Erlang/Elixir):

> {x, y} = p.GetCoordinates; # as a pattern match

Instead of a smoothly mentally-traceable flow of values into function arguments and out of function returns, we have this monstrosity of conflating mutable arguments (where values go in and then come back out changed in the scope of the function call) with this "out int x" business which seems to be indicating it intends to "push values upstream", as it were.

Re:New Features in C# 7.0 by louthy on Thursday March 09, 2017 @11:41PM (#13836430)
> int x, int y = p.GetCoordinates;

Obviously this can be done with the new tuple syntax, but in general I totally agree with everything you've said about 'out'. It is one of the ugliest language features ever, sometimes I wish MS would just say "To hell with backwards compatibility", and throw 'out' on the fire.

    // Function definition
    (int x, int y) GetCoordinates() =>
        (100, 200);

    // Invocation
    var (x, y) = GetCoordinates();
Re:New Features in C# 7.0 by pmarreck on Thursday March 09, 2017 @11:57PM (#13836493)
Well, to be charitable, fortunately you can use a style like you propose where values only flow as I've indicated... but you'd still have to contend with OPC (Other People's Code) and if the language allows it, you just know it will turn up somewhere and ruin your day! ;)
Re:New Features in C# 7.0 by louthy on Friday March 10, 2017 @12:05AM (#13836523)
Indeed. Although 'out' is rare, it occasionally does ruin my day. So I have created a set of extension methods [1][2] to remove them from my life (because it does physically enrage me whenever I see the need to use it).

[1] https://github.com/louthy/language-ext/blob/master/LanguageE...

[2] https://github.com/louthy/language-ext/blob/daa5a10028e9637b...

Re:New Features in C# 7.0 by mercurial on Friday March 10, 2017 @12:49AM (#13836676)
> but you'd still have to contend with OPC (Other People's Code) and if the language allows it, you just know it will turn up somewhere and ruin your day! ;)

Yes, like the standard library's Dictionary.TryGetValue()...

Re:New Features in C# 7.0 by yread on Friday March 10, 2017 @03:44AM (#13837223)
you can do

if (dict.ContainsKey(key)) return dict[key] else return null;

the only place where you can't avoid them are TryParse. And there it sort of makes sense - the value is sort of side effect

Re:New Features in C# 7.0 by louthy on Friday March 10, 2017 @06:43AM (#13837879)
> if (dict.ContainsKey(key)) return dict[key] else return null;

Which isn't thread-safe. So it's not always a better option.

Re:New Features in C# 7.0 by ygra on Friday March 10, 2017 @08:54AM (#13838546)
But Dictionary<TKey,TValue> isn't thread-safe anyway, unless not modified.
Re:New Features in C# 7.0 by louthy on Friday March 10, 2017 @09:04AM (#13838612)
Calling TryGetValue will either return a value or not. Calling ContainsKey, followed by an index read can lead to a race condition that means the value isn't there any more.
Re:New Features in C# 7.0 by ygra on Friday March 10, 2017 @10:21AM (#13839219)
If I'm reading the source correctly you could still get the bucket index and before you return true and the value, have that value removed from the dictionary. There is no synchronization going on, so concurrent modifications are just as much a problem with TryGetValue as with other approaches. TryGetValue is not atomic either.
Re:New Features in C# 7.0 by louthy on Friday March 10, 2017 @01:06PM (#13840642)
Ok, fair enough. My mistake. Glad I don't use the BCL collection types any more and stick to immutable maps, lists, and sets :)
Re:New Features in C# 7.0 by pmarreck on Friday March 10, 2017 @10:04AM (#13839076)
This is correct (and unfortunate).
Re:New Features in C# 7.0 by mrits on Friday March 10, 2017 @12:39AM (#13836645)
I feel like out was important for marshalling into C++ code?
Re:New Features in C# 7.0 by tdestan on Thursday March 09, 2017 @11:36PM (#13836409)
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?
Re:New Features in C# 7.0 by pmarreck on Thursday March 09, 2017 @11:39PM (#13836424)
Better. But the id1 argument can still be mutated by LookupName in this scope, no?
Re:New Features in C# 7.0 by tdestan on Thursday March 09, 2017 @11:46PM (#13836450)
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.

Re:New Features in C# 7.0 by louthy on Thursday March 09, 2017 @11:59PM (#13836504)
> 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#.
Re:New Features in C# 7.0 by benaadams on Friday March 10, 2017 @04:41AM (#13837446)
Mutable class with immutable struct wrapper would be better on allocations; then pass in the struct wrapper.
Re:New Features in C# 7.0 by ttd on Friday March 10, 2017 @05:21AM (#13837589)
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#.

Re:New Features in C# 7.0 by divanvisagie on Thursday March 09, 2017 @11:56PM (#13836482)
This language is called C# because you know... C. out variables are there because people were used to coding like this and now that it's there it would just break things to remove it from the language, so you might as well just make it a bit cleaner.
Re:New Features in C# 7.0 by pmarreck on Friday March 10, 2017 @12:01AM (#13836507)
Fair enough.

But people should stop coding like this. For the love of all that is good and binary, don't use arguments to mutate values in the same scope. Just return them and reassign them. That way, state flow is always in via the arguments and out via the return value(s). Without this discipline, you will have a hodgepodge of "value flows" and that complexity will kill you when debugging. When new values can be spit out both by function returns and function arguments... Hold on, I have to take an aspirin

I know that a lot of OO/procedural languages permit you to do this (unfortunately). Don't!

Re:New Features in C# 7.0 by divanvisagie on Friday March 10, 2017 @12:40AM (#13836649)
Yeah , I get you , this coding style is horrible. I personally try to avoid mutating things as much as possible. I wonder how long we will have these old artifacts around before we decide that things need to move to newer langs like F# instead of tacking new features onto the existing lingua franca of .NET.
Re:New Features in C# 7.0 by gokhan on Friday March 10, 2017 @01:42AM (#13836831)
Been using c# since the day 1, almost never seen someone designing from the ground up with out parameters for the last 10 years. They're not that common. I guess it looks like a favorite feature in the document because it takes the largest part of it, it's not.
Re:New Features in C# 7.0 by tigershark on Friday March 10, 2017 @10:54PM (#13844126)
Never used a concurrent dictionary or an IDictionary trying to be sanely efficient using TryGetValue instead of ContainsKey followed by a call to the accessor? I can't really believe that you started using c# from the day one and you never encountered an hot code path with a TryGetValue. Of course it gets much worse when you have millions of objects, but even with hundred of thousands it is kind of noticeable. Unless you don't care about caching at all, but that is a complete different kind of dragons'lair...
Re:New Features in C# 7.0 by mercurial on Friday March 10, 2017 @12:52AM (#13836686)
> out variables are there because people were used to coding like this

This does not invalidate OP's point that it is terrible design. You do not need to take every bad idea in older languages in the name of familiarity.

Re:New Features in C# 7.0 by olavk on Friday March 10, 2017 @02:29AM (#13836975)
I believe it is for interoperability with code in these languages, not just familiarity. That said, the BCL also use out parameters in a few places (the TryParse methods) which arguable set a bad precedent.
Re:New Features in C# 7.0 by spion on Thursday March 09, 2017 @06:55PM (#13835030)
Interesting, I've found the reverse is true for me with ES6.

For example, ES6 already had local functions (and a very terse form of that too using lambdas)

  class C {
    method() {
      let localFunction = x => x + 1
      function anotherLocal(a) { return a + 1; }
    }
  }
... arrays are normally used to represent tuples (or freeform objects), and it includes destructuring too:

  function f(x) { return [x, x*2] }
  let [x, xdoubled] = f(5)
or

  function f(y) { return {y, timesTwo: y*2} }
  let {y, timesTwo} = f(10)
It almost seemed to me that many of the features listed here were "borrowed" from the liberal style allowed by ES6 and TypeScript (am probably wrong, and both are borrowing from elsewhere).
Re:New Features in C# 7.0 by WaxProlix on Thursday March 09, 2017 @07:22PM (#13835193)
> (am probably wrong, and both are borrowing from elsewhere).

You're right there. Tuple unpacking has existed in even stronger form (pattern matching) in most functional or functional-ish languages for ages. Python's had unpacking since God knows when. F#, which is sort of related to ML and feeds features to C# somewhat regularly, does this sort of thing as first-class syntax.

It's sort of a trend for people who learn JS first (or only) to look at features that it's had stapled to it and then point out how much other languages are taking from it.

Re:New Features in C# 7.0 by spion on Thursday March 09, 2017 @07:52PM (#13835382)
You're incorrect in assuming that I've only/first learnt JS. But its sort of a trend for people to look down on JS programmers, since its considered a "low-status" language ;)

And I'm well aware that the direct inspiration for many of ES6's features is probably Python (and now C# with async/await, which in turn flowed there from F#)

I'm assuming some cross-borrowing is happening here due to developments on the TypeScript team, and due to much higher similarities of the specifics with TypeScript. After all, F# has been around for quite a while, yet we're only seeing this development now - so if F# is the inspiration, why did it not happen much earlier?

Re:New Features in C# 7.0 by WaxProlix on Thursday March 09, 2017 @08:30PM (#13835576)
Disagree strongly. I don't think looking down on JS programmers is a trend really, it's been happening for a long time now. If anything, that era has been fading with the legitimacy that runtimes like Node and adoption by high-profile companies bring to the JS ecosystem.

As for JS itself, ES6 is a great step in the right direction, and I like it a lot, though I prefer Typescript for pretty much anything when I can use it.

Also - I didn't say that you learned it first or only, just that I see a lot of praise for 'JS features' being adopted by other languages from people who very obviously don't know any better - and this only in the last 3 or so years. So I think it's a trend.

Re:New Features in C# 7.0 by spion on Thursday March 09, 2017 @08:40PM (#13835626)
Oops sorry - I meant that tongue-in-cheek as realised I was getting too defensive.
Re:New Features in C# 7.0 by jackfoxy on Thursday March 09, 2017 @08:03PM (#13835442)
It didn't happen much earlier because the Microsoft languages team is very deliberate and careful about rolling out new features. They have to be. There are something like 1.5 billion machines running some sort of Windows, and (I don't know) on the order of 1/2 - 1 million developers who work with MS languages and tools. Lots of backward compatibility to think about. At this scale even five 9s quality ends up breaking things.
Re:New Features in C# 7.0 by DougWebb on Thursday March 09, 2017 @07:42PM (#13835326)
Python's had unpacking since it got it from Perl, back in the 90's. I'm really glad to see C# is catching up :)
Re:New Features in C# 7.0 by ZeroClickOk on Thursday March 09, 2017 @07:10PM (#13835117)
Each release c# get more and more "functional". Previous versions you can use anonymous methods or lambdas to "simulate" local functions, but now its first class citzen.

The problem with JS is you need deal with a lot of hacky legacy things too, you want try new cool things on ES6/ES7, but you need take care about compatibility, 3rd party libraries integration, etc.

IMHO, I hope something like "ES10", where they forget previous versions, create from scratch a new javascript, without worries about polifilling, compatibily, shimming, etc. Then start to evolve from there.

Re:New Features in C# 7.0 by dodyg on Friday March 10, 2017 @02:32AM (#13836979)
It's not wise to start a over a language. Perl6.
Re:New Features in C# 7.0 by int_19h on Thursday March 09, 2017 @10:19PM (#13836065)
The borrowing kinda goes in circles. The terse syntax of ES6 lambdas is straight from C#, for example.
Re:New Features in C# 7.0 by jfroma on Thursday March 09, 2017 @09:30PM (#13835852)
I worked for 6 years on C#, I was C# MVP and I really enjoyed the language.

4 years ago I started to work more with js and then node.js until I stopped working on c#. Js has been for me a gateway to transition to other languages and get paid for that. I learned bash, python, a bit of ruby, go, etc.

If I see c# code today I don't understand what it does. Sure every version has a lot of new very useful stuff but it also introduces noise, "why he used out x instead of the new shiny out var x, he might not know about it, lets send PR".

Along the way I have learned to appreciate simpler languages that does not change that often. Where there are few or just one way to do something.

I think I read an interview to Mitchel Hashimoto (hashicorp) and he said he choose Go over all languages because it was boring.

A boring and simple language allows you to focus on the problem you are solving. A language that doesn't change for years allows people to catch up and master the tool. It also means that you can open a file written by someone else years ago and understand what it does.

IMHO js was in that sweet spot until ES6.

Re:New Features in C# 7.0 by ShinTakuya on Friday March 10, 2017 @03:41AM (#13837207)
Not only that but a lot of very questionable programmers use features just because they're there. C# is becoming like C++ in the sense that it can enable beautiful code from the good coders and truly terrible code from the bad coders.
Re:New Features in C# 7.0 by buzzybee on Thursday March 09, 2017 @11:57PM (#13836492)
Go is indeed attractive for this. Although it's a little bit too far to the end of "no extendability" for me. Oberon-07 and Lua are also great examples of making it work.
Re:New Features in C# 7.0 by mattchew on Thursday March 09, 2017 @11:34PM (#13836396)
I agree. After watching the Go developers push back against noisy changes, I'm much more appreciative of determined simplicity in language design.

> If I see c# code today I don't understand what it does.

I am not surprised. The C# team should be more concerned about this. I love C#, but it is getting more complicated and arcane and therefore more unfriendly to newbies. Some of this is probably unavoidable, but some of it could be avoided. The language has to stay attractive to newcomers to survive in the long run.

Re:New Features in C# 7.0 by swlkr on Thursday March 09, 2017 @10:01PM (#13835982)
You should give clojure a try, the community resists change not because their luddites, but because change is risk.
Re:New Features in C# 7.0 by untog on Thursday March 09, 2017 @08:02PM (#13835434)
You're fighting JS more than you fight C# because you don't know JS as well as you know C#.

As someone who has coded in both I won't deny for a second that C# is the better language, but I spend most of my time in JS these days I don't "fight against" it at all. I know how it works, and I use it accordingly.

Re:New Features in C# 7.0 by ralusek on Thursday March 09, 2017 @07:09PM (#13835098)
What are you talking about?

What are you finding unnatural about the direction JS has gone in?

    class Guy {
      sayHello() {
        console.log('Hello.');
      }
    }

    const guy = new Guy();
    guy.sayHello();
Or for practical code: Get IDs of users named Mike

    db.users.fetch({firstName: 'Mike'}, {limit: 10})
    .then(users => users.map(user => user.id));
The language is so popular to shit on, with absolutely no basis.
Re:New Features in C# 7.0 by dested on Thursday March 09, 2017 @06:57PM (#13835040)
I'm a huge fan of http://bridge.net/ for c# to js "transpilation". The runtime isn't too hefty and provides great feature parity to javascript.
Re:New Features in C# 7.0 by ken47 on Thursday March 09, 2017 @08:24PM (#13835543)
Javascript has taken a quantum leap with es6, and if you look at what Typescript and Flow are doing, another quantum leap in that dimension. C# is great, but I think the latter, especially Typescript, bring the Javascript community into the same ballpark.
Re:New Features in C# 7.0 by kevin_thibedeau on Thursday March 09, 2017 @06:59PM (#13835051)
I wouldn't be surprised if Typescript becomes native in the next decade. That will address some of the sins of JS and make Webassembly less of a hack.
Re:New Features in C# 7.0 by klodolph on Thursday March 09, 2017 @07:10PM (#13835111)
I emphatically disagree with the assertion that WebAssembly is a hack. Maybe asm.js is a hack... but WebAssembly is fairly well thought out.

I don't expect TypeScript to compile easily to WebAssembly. You'd have to write your on GC and do a bunch of other work that you get for free when you target JavaScript... and that means that there are plenty of opportunities do make your runtime worse than your target browser's runtime, at the cost of increased code size to boot.

Re:New Features in C# 7.0 by cwyers on Thursday March 09, 2017 @07:59PM (#13835412)
The goal is for WebAssembly to get access to the same GC as JavaScript eventually, right? Not mandatory, but available.
Re:New Features in C# 7.0 by johnfn on Thursday March 09, 2017 @08:39PM (#13835619)
It's funny you say that, because as someone more experienced in JS, I found myself saying "oh look, C# is coming up to par with ES6!" e.g. Tuple types and destructuring, which are a part of ES6.
Re:New Features in C# 7.0 by louthy on Thursday March 09, 2017 @08:51PM (#13835677)
> Tuple types and destructuring, which are a part of ES6.

And part of many, many languages prior to ES6.

Re:New Features in C# 7.0 by johnfn on Thursday March 09, 2017 @09:24PM (#13835821)
I don't see how bringing this up is necessary or relevant. These types of comments seemed to me to be nothing more than a snarky aside. "You're happy about X feature? Well it already existed in Y language."
Re:New Features in C# 7.0 by louthy on Thursday March 09, 2017 @09:45PM (#13835910)
I brought it up, because the the original post seems to be implying that C# is just copying ES6, and only now "coming up to par". Whereas I think anyone who's not caught in the JS bubble will realise it's very unlikely to be true. There are other .NET languages that have tuples and destructuring, never mind the myraid other languages that have had those features for decades; and ES6 is hardly a panacea when it comes to languages. It's certainly not something that a language designer would be trying to 'come up to'.

Apologies if you thought my comment was snarky, it wasn't my intention.

Re:New Features in C# 7.0 by johnfn on Thursday March 09, 2017 @10:23PM (#13836090)
I don't see why everyone is so defensive about languages, though. C# can achieve parity with ES6 in some ways, and ES6 can achieve parity with C# in others. It doesn't necessarily imply that the developers of one language were cribbing notes from another. It's just a happy thing that some of my favorite features are seeing more widespread adoption between languages.

With my original comment, I was trying to imply that the context in which you read about languages (e.g. which languages you are the most familiar with) can color how you perceive the news. Sadly I feel like everyone read it as an indictment against C# or something.

Anyway, my bad for mistakenly calling you out. It's a bit of a reflex after seeing so many people try to claim that their language is superior.

Re:New Features in C# 7.0 by nulagrithom on Thursday March 09, 2017 @08:50PM (#13835667)
I thought the reverse with ES6. :) "Oh look, arrow functions! Finally!"

It's fun to see how languages influence each other.

Re:New Features in C# 7.0 by johnfn on Thursday March 09, 2017 @09:25PM (#13835824)
Hah, that was actually ES5 ;-) but I assure you the entire JS community was celebrating. :)

I actually think C# is a better language than JS, but it's nice to see how they get the best features from each other.

Re:New Features in C# 7.0 by nulagrithom on Thursday March 09, 2017 @10:00PM (#13835978)
Re:New Features in C# 7.0 by johnfn on Thursday March 09, 2017 @10:20PM (#13836074)
Weird, it feels like they've been around longer... too many transpilers.