13 ms·
Out variables seem like a mis-feature, especially when they are adding a better alternative (Tuples) in the same release. It's great to finally have tuples, bu
by flukus 10y ago
Out variables seem like a mis-feature, especially when they are adding a better alternative (Tuples) in the same release.
It's great to finally have tuples, but the c/java style syntax is showing it's age compared to something like scala which has return type declarations at the end, which I find much more readable.
Literal improvements will be a godsend for writing database migrations.
Ref returns and locals look like a source of constant headaches. It's much harder to reason with code when the data can be modified by random others bits of code. Currently I can look for all references to an array to find everywhere that modifies it now I can't.
- clusmore 10y ago>Out variables seem like a mis-feature, especially when they are adding a better alternative (Tuples) in the same release. You can't retro-fit multiple-returns (via Tuples) onto existing library functions, so for places where you're forced into using out parameters, this is a slight improvement.
- jameshart 10y agoSure you can - you can introduce a new overload resolution rule that lets you treat trailing out params on a method as tuple members, so a method TReturn Foo(T1 param1, out T2 param2) can be called as if it were (TReturn, T2) Foo(T1 param1) You'd need a keyword to trigger this kind of overload resolution at the call site to ensure back compatibility - maybe you'd have to call (var x, var y) = out Foo(param); or something. That way all that legacy library code gets a free upgrade to your new language feature. Since the C# team decided not to do that, you can actually implement a version of this at a library level, by static importing a family of generic methods like this: Func<TParam, (T1, T2)> Tuplify<TParam,T1,T2>(Func<TParam, out T1, T2> outFunc) {...} for every pattern of out params you want to convert, then you can just call (var x, var y) = Tuplify(Foo)(param);
- deleted 10y ago[deleted]
- merb 10y agoI've seen some p/invoke examples where out variables are used as a c pointer.
- cm2187 10y ago> it's age compared to something like scala which has return type declarations at the end, which I find much more readable. Scala or .... VB.net!
- flukus 10y agoI'll begrudgingly admit it got a couple of things right.
- AnthonyDGreen 10y agoLMAO -Anthony D. Green, Program Manager, Visual Basic
- daigoba66 10y ago> Literal improvements will be a godsend for writing database migrations. Can you elaborate on this; both how it helps and why you're using C# for database migrations?
- flukus 10y agoMy go to migrator is fluent migrator (https://github.com/schambers/fluentmigrator https://github.com/schambers/fluentmigrator) or flywaydb if anyone objects to the c# (https://flywaydb.org/ https://flywaydb.org/). Each migration gets a number, typically time encoded. If I was to write one now it would have the attribute [Migration(201608251157)]. With the new literals this will become [Migration(20160825_1157)], amazing how much readability a single underscore can make.
- noblethrasher 10y agoOut parameters are also useful in constructors. I often use them as a way to limit the scope in which an object can be modified, which lets me have mostly immutable objects. IEnumerable<Student> GetStudents(SqlCommand cmd) { var rdr = cmd.ExecuteReader(); //select * from student left join courses on... var memo = new Dictionary<int, Student.Completion>(); //Completion lets us add courses to a student while(rdr.Read()) { if(!memo.TryGetValue(rdr.GetInt32("student_id"), var out complete) memo.Add(new Student(rdr out complete).ID, complete); complete.AddCourse(rdr); //might be a no-op } return from kv in memo select kv.Value.Student; //each student object is unmodifiable. }
- 16bytes 10y agoSo I'm taking a look at this line: if(!memo.TryGetValue(rdr.GetInt32("student_id"), var out complete) memo.Add(new Student(rdr out complete).ID, complete); I think I understand your point, but I found this code really hard to read. You don't use the first var out complete in the TryGetValue, right? And the Student c'tor returns itself as an out parameter? If I understand you correctly, you like this because you don't have to declare a new student before you add it? I.e. the alternative would be if(!memo.ContainsKey(rdr.GetInt32("student_id"))) var student = new Student(rdr); memo.Add(student.ID, student); I guess I would prefer this over the former. Also, why not enforce your student ID constraint in SQL instead of putting everything in a dictionary only to take it back out again? That would simplify your code to the point of just being a map from rdr->Student. Furthermore, if all I had was the Student c'tor, I would never guess that was the intent of the out parameter. This seems more anti-pattern than pattern. I would rather put a simple IEnumerable in front of SqlDataReader so that you could just do: foreach(var row in rdr) yield return new Student(row) This doesn't obviate your use case, however, which is to inline a variable where it's needed in multiple places in that line because you can save yourself an explicit declaration. In this case, however, I think that the increased readability justifies the explicit declaration.
- noblethrasher 10y agoHi, thanks for taking the time to comment. > You don't use the first var out complete in the TryGetValue, right? There is only one complete variable; we declare it in TryGetValue, and it's definitely used in the last line of the while statement, but might first be used after the if statement. > And the Student c'tor returns itself as an out parameter? The Student constructor does not return itself as an out parameter, rather it returns an object that can modify an internal list of courses. The idea is that when we iterate through a result set from a database, some of the rows are going to correspond to a new student object, and some are going to correspond to a course that belongs to the student. Crucially, we are only allowed modify the Student object (or whatever) during iteration, and the collection that we return will only contain immutable/unmodifiable Student objects. I've used this technique to populate deeply nested structures (lots of joins and nested joins) using only one query. > I would rather put a simple IEnumerable in front of SqlDataReader so that you could just do: > foreach(var row in rdr) yield return new Student(row) Just to be clear, the reason that I cannot do that is because the Student object might not be completely "hydrated" until we finish iterating through the result set, because it might contain a bunch of nested objects that also need to be instantiated from one or more DB records. > Also, why not enforce your student ID constraint in SQL instead of putting everything in a dictionary only to take it back out again? I'm not quite sure I understand this (which is probably my fault), but rest assured that we use nothing but SQL (specifically, DDL) to enforce data integrity.
- mhomde 10y agoYeah, I think lots of uses of out in the .Net framework are from the early days and a little bit of a mistake. Since they can't remove them this is suppose to make them easier to work with. TryGetValue comes to mind, but I agree overall out variables always felt clunky
- douche 10y agoAlthough F# manages to convert all those TryGetXXX methods into tuple-returning transparently, so it can be done. Just off the top of my head, you could probably write an extension method to wrap the original into the new format pretty easily.
- CyrusNajmabadi 10y agoHey there, C# language designer here. > Out variables seem like a mis-feature, especially when they are adding a better alternative (Tuples) in the same release. Out variables are really great when working with existing code that predates Tuples (for example, the entire BCL). We don't just introduce language features that will only work well for new code. We also want to make the experience of coding against existing APIs feel better. > Ref returns and locals look like a source of constant headaches. That 'headache' has to be weighed against the needs of some parties to write very fast code and to have as little overhead as possible. ref returns enables a much more pleasant coding experience that enables these sorts of speedups in these specialized scenarios. > It's much harder to reason with code when the data can be modified by random others bits of code. There is a simple solution to this of course, don't expose your data through ref returns :) For many (likely most) developers, ref returns simply won't be something that ever hits their radar. Similar to stackallocs and other esoteric features, this is really a targeted improvement for people who really like working in .Net but are finding it hard to squeeze out essential perf in some core scenarios. The .net runtime supports this features fantastically. We wanted to make it available to people who like the power and safety of C#, but occasionally need some low level hooks to get that extra perf boost as well.
- int_19h 10y agoDid you guys look into alternative syntax for return types? It really is a huge eyesore, and impediment to reading, when you have more than just a typename and a couple of modifiers like * and [] attached to it. With tuples especially, it's even worse, because the method name gets squished between two very similarly looking ()s, which is very different from how methods have historically looked in code. If I were scanning code fast, I'm not sure I would even read it as a function declaration. C++ adopted its "auto ... -> ..." syntax a while ago - granted, they had a forcing function in form of decltype(), but many people also use it for readability reasons with complex return types. I hope C# follows suit; or, better yet, comes up with a unified type-after-name syntax a la ES6, that can be used everywhere in the language, while retaining existing syntax for back-compat purposes.
- 10y ago
- mohdmasd 10y agoout/ref still have their uses when interfacing with COM or P/invoke.
- cholantesh 10y ago>c/java style syntax is showing it's age compared to something like scala which has return type declarations at the end, which I find much more readable I hope there are better examples of Scala's syntactical advantages than this one, because it seems like the 'egyptian-style' vs 'next line style' brace bracket debate...
- premium-concern 10y agoYes there are plenty. The correct order of type declarations just ties in with the better syntax for generics and leads to an easier to read, more consistent language.
- cholantesh 10y agoInteresting; thanks. F# and Scala are pretty high on my to-learn list.