8 ms·
So happy out variables are finally a thing, removes such a pain point in the language, especially when having to write: SomeReallyLongTypeNameICantQuiteRem
by hacker_9 10y ago
So happy out variables are finally a thing, removes such a pain point in the language, especially when having to write:
SomeReallyLongTypeNameICantQuiteRemember data;
if (dict.TryGetValue(id, out data)) { ... }
Now reduced to:
if (dict.TryGetValue(id, out var data)) { ... }
- flukus 10y agoWhile it's good in that example, in the real world I'm expecting a tonne of bad code to use it as a multiple return mechanism.
- JosephRedfern 10y agoSurely tuples should get rid of the need for that hack?
- simcop2387 10y agoThat's what I'd hope, but I've seen in other languages where instead it ends up error_type function(out data, out data2, ...); instead. Given that C# has proper try/catch and other mechanisms of error handling I'd hope that this pattern won't repeat itself.
- flukus 10y agoI hope so, but I expect there existence to filter out a lot more slowly.
- hermitdev 10y agoSure for new code, but I think the concession here is that it provides convenience for dealing with existing code/libraries. Additionally, it allows restricting the scope of the variable, like in the TryParse examples. The output variable in something like "if (int.TryParse(someString, out var i))" is now scoped to the if block. It cannot be used in the else, nor inadvertently later in the parent scope.
- revelation 10y agoThis release also introduces new syntax for tuples (closer to Python) that makes it much more transparent to use them and pretty much thereby introduces multiple return values. This is now valid and returns a tuple with named values: public (string city, string state) GetCityAndState() { return ("Lake Charles","Louisiana"); } And you can get the return values without writing "Tuple<" once.
- maxxxxx 10y agoThis is nice!
- janwillemb 10y agoOut parameters are not new in C# 7, only the shortened usage syntax is. So the bad code you're expecting is already there.
- flukus 10y agoIt is ;) I'm just expecting it to get worse.
- bbcbasic 10y agoOut variables can be used for 'while were at it' code - separate concerns that are handy to code in the place where the data is available locally but have no place to be there.
- JamesNK 10y agoI'm expecting it won't. With C# 7 tuples there will be less methods with out parameters in the future.
- contravariant 10y agoIs using out for returning multiple values bad? Tuples seem the only good alternative, and those have only just been introduced.
- gnaritas 10y agoYes, it leads to lots of bad procedural code. If you need multiple data items returned from something, they are clearly related and belong in an object. Well written code doesn't use out parameters at all, they are a bad feature and should be removed from the language.
- barrkel 10y agoIf it's natural for a mathematical function to return a tuple, it should return a tuple; things being objects for the sake of it doesn't constitute an argument. For example: coordinates, swap function, a list partitioning function (returning two lists partitioned based on an element), etc.
- gnaritas 10y agoA tuple is an object; it's a perfectly proper way to return multiple values from a function, out parameters are not. In fact I'm unsure what your comment has to do at all with anything I said, I'm discussing out parameters and why I don't like them and why it's better to return and object and then you say no return a tuple, which is an object, an instance of the class Tuple. You seem to be trying to disagree with my statement but you've actually supported my point.
- barrkel 10y agoOnly an object-oriented programmer will say that a tuple is an object. A relational programmer or someone using a logic programming language wouldn't say the same thing. A tuple doesn't have behaviour, inheritance, encapsulation, or data hiding. Tuples are language-agnostic. An OO language has little choice but to represent tuples as objects, but to say that tuples are objects is no more true than saying that a user's account is an object, a TCP connection is an object, or a window on screen is an object - it mistakes representation for referent, it confuses the map with the territory.
- maxxxxx 10y agoI wish you could somehow define a return type for a function without having to declare a class. Maybe have the function return an anonymous class and then have the compiler infer that.