开发者

Is it possible to convert F# code to C# code?

开发者 https://www.devze.com 2023-02-06 21:36 出处:网络
The reason why I am asking is that I\'m learning F# and would like to attend TopCoder competitions. However, F# is not among the list of languages supported there. But C# is on the list (to be honest,

The reason why I am asking is that I'm learning F# and would like to attend TopCoder competitions. However, F# is not among the list of languages supported there. But C# is on the list (to be honest, this is the case for almost all online coding competitions, except Google Code Jam and Facebook Hacker cup).

The possible workarounds I can think of at this moment are 1) find a translator that can translate F# source code directly into C# 2) compile F# code into .net executable first, then disassemble it back to C# co开发者_如何学JAVAde

The minimum requirement is that the generated C# must be able to compile into a runnable .net executable, preferable as less external dependency as possible.

The first approach seems unlikely, a quick google search turns out nothing relevant. Approach two looks more promising, there are .net disassemblers exist.

I tried the most popular one --- Reflector from Red Gate. While it can perfectly dissemble C# executables, it appears to have problems with executables compiled from F#: it happily disassembled, but the resulting C# code has some special characters such as adding a leading $ sign to a class name and other weird stuffs, so it cannot be compiled. I was using Visual Studio 2010 Professional, the latest Reflector beta version (which is free).

Am I missing anything here? Is it possible?

Update: It looks like this is still impossible. For now, I'll use C# instead.


As others already pointed out in the comments - if there is some way to do that, there will be quite a few nasty cases where it probably won't quite work and it will be very fragile...

One way to deal with the problem (for you) is to just write the solution in F# and then rewrite it to C#. This may sound stupid, but there are some advantages:

  • In F#, you can easily prototype the solution, so you'll be able to find the right solution faster.
  • When translating code to C#, you'll probably find yourself using features like lambda expressions more often, so it may even improve your C# skills...
  • If you rely on .NET libraries, then this part of code will be easy to translate.

Of course, the best thing would be to convince the organizers that they should support F# (which probably wouldn't be too difficult if they allow C# already), but I understand that this may be a challange.

0

精彩评论

暂无评论...
验证码 换一张
取 消