microsoft / qsharp-runtime

Runtime components for Q#
https://docs.microsoft.com/quantum
MIT License
286 stars 93 forks source link

Some valid Q# callable names produce invalid C# code #241

Closed bamarsha closed 4 years ago

bamarsha commented 4 years ago

The classes generated for Q# callables contains several built-in members with names that are not surrounded by __. This means they are valid Q# identifiers. If you declare a Q# callable with the same name as one of these members, the C# code does not compile because the member name conflicts with the constructor name.

Here is an example:

function Init() : Unit {
    Message("Hello quantum world!");
}

produces this C# compile error:

Program.qs(22,30): error CS0542: '"Init": member names cannot be the same as their enclosing type.

Here is a list of names I tried that are valid Q# but produce invalid C#:

bamarsha commented 4 years ago

My idea for fixing this is to surround all members of callable classes with __, but add extension extension methods on the Operation<TIn, TOut> or ICallable types with the original names, since extension methods are allowed to have the same name as the class. If we add the extension methods to a namespace that most Q# programs already import, this should be mostly seamless, but technically not 100% backwards compatible unless there's a way to say that a class should always import some associated extension methods (I don't know of a way to do that though).