Skip to content

Top level statements and member declarations (embrace scripting dialect) (VS 16.8, .NET 5) #2765

@MadsTorgersen

Description

@MadsTorgersen

The C# compiler currently understands a dialect of the language used for various scripting and interactive purposes. In this dialect, statements can be written at the top level (without enclosing member bodies) and non-virtual members (methods etc) can be written at the top level (without enclosing type declarations).

Usage of the scripting dialect has been relatively minor, and in some ways it hasn't kept up with the "proper" C# language. However, usage is picking up through Try.NET and other technologies, and I am concerned with being stuck with two incompatible dialects of C#.

I think there is merit to allowing top-level program code instead of requiring the "program" to be in a Main method. And I think there is merit to allowing functions to be declared at the top level of a program, without needing to be enclosed in a class. I think these allow small programs to be meaningfully simpler, and make the language easier to learn.

public int Fac(int x) => x < 0 ? x * Fac(x-1) : 1; // Top-level function
Console.WriteLine(Fac(5)); // Top-level statement

As opposed to:

public class Functions
{
    public int Fac(int x) => x < 0 ? x * Fac(x-1) : 1; // Top-level function
}
public class Program
{
    static void Main()
    {
        Console.WriteLine(Functions.Fac(5)); // Top-level statement
    }
}

I believe we should consider putting these extensions into C# "proper" and do away with the separate scripting dialect. In doing so we should not feel too bound by design details of the scripting dialect, but make sure we resolve those details in a way that's best for C# as a whole. I'm not so worried about "breaking" the C# scripting dialect. Since its use is mostly interactive, there probably isn't going to be all that much source code around that depends deeply on its semantics.

I hope that adding these features can help create a more continuous growth path from someone experimenting in a Jupyter workbook to having a full-blown C# application, and that sort of scenario.

I don't have a concrete fleshed-out proposal at this point. I think we should start by reviewing the current state of affairs and brainstorming what would work well and less well from the scripting dialect.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions