C # language extension, how much effort or effort?

Introduction

As a developer, I participate in writing a lot of mathematical code every day, and I would like to add very little syntactic sugar to C # to make writing and viewing code easier.

I have already read about this thread and this other one for possible solutions and just wanted to know what is the best direction and what efforts he can solve to solve only the following three syntax problems *.

*: I can survive without the syntactic sugars described, but if this is not too much work and Roubaix-Goldberg design for a simple compilation process, it may be interesting to explore further.

1. Several output arguments

I would like to write:

[double x, int i] = foo(z); 

Instead:

  double x; int i; foo(out x, out i, z); 

NB: out parameters are placed first, and foo declared as usual (or the same syntax is used).

2. Additional operators

I would like to have some new unary / binary operators. I don’t know how to determine for them (and this seems rather complicated in order not to introduce ambiguity in the analysis of sources), in any case I would like to have something like:

 namespace Foo { using binary operator "\" as "MyMath.LeftDivide"; using unary operator "'" as "MyMath.ConjugateTranspose"; public class Test { public void Example() { var y = x'; var z = x \ y; } } } 

Instead:

 namespace Foo { public class Test { public void Example() { var y = MyMath.ConjugateTranspose(x); var z = MyMath.LeftDivide(x, y); } } } 

3. Automatic name insertion for static classes

It is extremely unattractive to endlessly repeat Math .BlaBlaBla () everywhere in the computational code instead of writing directly and simply BlaBlaBla.

Of course, this can be solved by adding local methods for transferring Math .BlaBlaBla inside the calculation class. In any case, it would be better if there was no ambiguity at all, or when the ambiguity was resolved using some implicit keyword, to automatically insert class names if necessary.

For example:

 using System; using implicit MySystem; // Definying for 'MyMaths.Math.Bessel' public class Example { public Foo() { var y = 3.0 * Cos(12.0); var z = 3.0 * Bessel(42); } // Local definition of 'Bessel' function again public static double Bessel(double x) { ... } } 

It would be:

 using System; using MySystem; // Definying for 'Math.Bessel' public class Example { public Foo() { var y = 3.0 * System.Math.Cos(12.0); // No ambiguity at all var z = 3.0 * MySystem.Math.Bessel(42); // Solved from `implicit` keyword } // Local definition of 'Bessel' function again public static double Bessel(double x) { ... } } 

* The compiler can simply generate a warning to indicate that it resolved the ambiguity, since an implicit solution was defined.

NB: 3) satisfies enough for solution 2).

+8
c #
source share
2 answers

I would be one of the first to support the main C # thread, which can be extended by users. However, for several reasons (design, time or cost) I don't see C # being as extensible as you ( or I ). C # -cooled language I found that is very good for metaprogramming and extending Nemerle functionality / syntax.

Boo is another .NET language with good metaprogramming features. However, it is so far removed from C # that I did not find it a suitable answer to this question (but still added it for completeness).

+3
source share

Have you considered using F # instead of C #? In my experience, I found F # to be great for science / math oriented code, not C #.

Your first scenario is covered in tuples; you can write a function like let fab = (a+b, ab) that immediately returns a tuple of two values, you can quite easily overload the operators or add your own, and the modules can help you with the third one. And F # interacts pretty smoothly with C #, so you can even choose F # for the parts where it’s practical, and save the rest of your C # code. There are other subtleties that are great for scientific code (for example, units ).

+7
source share

All Articles