Replies: 98 comments 15 replies
-
👎 for variables, 👍 for functions. |
Beta Was this translation helpful? Give feedback.
-
@dbettin I think you should break this into two separate issues but I don't think that "free function" is going to happen because you can use Something like this:
And then in another place you can just do To global variables I say a big NO. |
Beta Was this translation helpful? Give feedback.
-
These are not Global variables. Variables would still be scoped to some container (namespace./module). Also, yes, this is possible today with static methods. But, imagine how clean this approach is compared to always creating public static classes/methods. |
Beta Was this translation helpful? Give feedback.
-
In practice, what's the difference between a member that's directly in a namespace and a member of a static class in the global namespace? In other words: using System.Collections.Generic;
namespace Awesome
{
const int MY_NUMBER = 5;
List<string> MyList = new List<string>();
void AddToMyList() => MyList.Add(MY_NUMBER + 1);
} vs. using System.Collections.Generic;
static class Awesome
{
public const int MY_NUMBER = 5;
public static List<string> MyList = new List<string>();
public static void AddToMyList() => MyList.Add(MY_NUMBER + 1);
} Is the main difference that you don't have to write |
Beta Was this translation helpful? Give feedback.
-
I really don't know what you're asking then and @svick already said what I was about to say so really, not sure about the differences except keywords and the "plumbing" work that almost doesn't exist and in practice is a very, very small price to pay. :) |
Beta Was this translation helpful? Give feedback.
-
I am looking for a discussion around the inclusion of functions not methods into C#. As C# continues to evolve into a dual purpose language (records, pattern matching, discrimated unions; to name a few), functions are a key abstraction for the functional side of the language. There is a reason why Go, Rust and Kotlin have them as part of their respective languages. Just look at extension methods as you will see the nonsense that is the "static class wrapper". Or any static class that only contains methods. Why not make the language less ceremonial? C# has made existing features less verbose in the past and because of that, has given us a better language to read and write. |
Beta Was this translation helpful? Give feedback.
-
Changed the title so people can focus the discussion on functions only. |
Beta Was this translation helpful? Give feedback.
-
You are speaking to the user of these methods. I am specifically talking about the author and/or reader of the extension methods. |
Beta Was this translation helpful? Give feedback.
-
The "and" is the that it doesn't address the critique of extension methods wrapped in a static class. My critique is in reference to how you author them not in how you use them. |
Beta Was this translation helpful? Give feedback.
-
I think is better Idea to include a "Module Initialization" to an assembly as DELPHI does. |
Beta Was this translation helpful? Give feedback.
-
Even if we did go for namespace-level functions, I'd still want them to be |
Beta Was this translation helpful? Give feedback.
-
I think this would remove a lot of needs for namespace FooApplication
{
using static System.Console;
void Main() => WriteLine("Hello World!");
} I think this would be great because usually the |
Beta Was this translation helpful? Give feedback.
-
Agreed. It has always confounded me that the |
Beta Was this translation helpful? Give feedback.
-
@whoisj Would you say the same thing about C++, since global variable initializers execute before |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Ha, if even VB.Net has modules, there's no real excuse not to provide them in C# too 🤣 |
Beta Was this translation helpful? Give feedback.
-
As you know, CIL supports global functions and in C# they appears as members of Just in case, note that namespace scooped function may provide slight interoperability for other .Net family but module won't (although it is doubtful that such languages exist). |
Beta Was this translation helpful? Give feedback.
-
Wait, really? I've been using the CIL reader API for a while and don't know how that would even work without a CIL class. |
Beta Was this translation helpful? Give feedback.
-
As far as I can tell, the answer is "kind of". ECMA-335 §II.10.8 Global fields and methods explains how global members are represented as members of the special class But if you're reading the IL, it looks almost like any other class. |
Beta Was this translation helpful? Give feedback.
-
I'm curious about this statement of yours:
I'm struggling to imagine what it means for something to be private within a namespace. I think that would be a new concept for C#: currently this is not legal: namespace MyNamespace
{
private class Foo
{
}
} Even if it was legal, what does it mean? That members and types defined in |
Beta Was this translation helpful? Give feedback.
-
I could simply be confused of course, but my thinking was that even for namespace-level functions, or module-based functions, they still end up being lowered to static methods in some class (which could be For any set of free functions, I'd want some of them to be implementation details (ie, private). Since the default behaviour is for functions/methods to be private unless annotated otherwise, then it makes sense to me (until persuaded otherwise, of course) that free functions should default to private, too. |
Beta Was this translation helpful? Give feedback.
-
This is true, and therefore your suggestion makes sense, but I would actually be in favour of hiding this from the user. I would want it look and feel, as much as possible, like the functions actually just exist on their own in a namespace. As for implementation details, we still have local functions which will solve the majority of cases. I'm open to having my mind changed on this one, since even F# allows private values in modules: module Stuff =
let mutable private state = 0
let f() =
let result = sprintf "%A" state
state <- state + 1
result
open Stuff
f() |> printfn "%A"
f() |> printfn "%A"
f() |> printfn "%A"
f() |> printfn "%A" I still think though that the default should be public. I know that in C# classes the default is public, but let-bindings are private in F# classes but public in modules and everyone seems to cope just fine. C# is also introducing default interface methods and in interfaces members are public by default†, so there's precedent. † I haven't had a chance to play with the default interfaces feature yet; is the |
Beta Was this translation helpful? Give feedback.
-
I support this idea. Wish we could write the actual only one |
Beta Was this translation helpful? Give feedback.
-
@Thaina You could still have two within an assembly if you have two namespaces 😉 |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h Well then I think we should let us just write it without namespace and make it default namespace |
Beta Was this translation helpful? Give feedback.
-
Hey is there any progress here? |
Beta Was this translation helpful? Give feedback.
-
Today you can define "global functions like": global using static GlobalFunctions;
using System;
public static partial class GlobalFunctions{
public static void SayHello(){
Console.WriteLine("Hello");
}
} And use it directly e.g.: SayHello(); I personally see no need to extend this further - I may agree that the declaration of a "global function container" seems not needed but it helps to avoid name clashes and global namespace pollution. |
Beta Was this translation helpful? Give feedback.
-
@aradalvand I don't think that your examples are in the same ballpark as this one. Examples you've given are saving these few braces and an indentation level per file for everybody using the language. That's a lot of braces and indentation levels. It maybe seem small but benefit is muptiplied greatly. I'm not sure this one is ever going to be as useful pr influential as in "wow, we've got And one more thing - these features don't introduce weird naming clashes and overload priority issues simply by |
Beta Was this translation helpful? Give feedback.
-
I have read the following: https://blogs.msdn.microsoft.com/ericlippert/2009/06/22/why-doesnt-c-implement-top-level-methods/.
But, it is time that C# joins every other modern language (Go, Rust, F#, Kotlin, Swift, ...) and supports top-level functions. The amount of ceremony that goes away with this addition would be fantastic.
Thoughts (and sorry, if I missed an existing issue that is relevant to this one)?
Edited: Removed Top-level variables proposal
Beta Was this translation helpful? Give feedback.
All reactions