In C#, I can leverage code snippets as shortcuts to produce plumbing code quickly. Is this supported in F#?
For example, if I want to implement property getters and setters, can I do this? In C#, I could just type:
propTAB
What options are available in F#?
This feature may be added as VS extension.
Related
If I'm familiar with F# events already, and I don't plan to have too much C# interop, are there significant reasons to consider using the Reactive Framework?
I think there are two main differences:
Firstly, there is a difference between the IEvent<'T> interface (and functions in the Event module) and the IObservable<'T> interface (used by functions from the Observable module and Reactive Fx). The difference has been discussed on SO earlier.
Reactive Framework is a more complex library, so it implements many combinators that are not available in F# in Observable or Event modules (although there is a open-source project that adds many of them)
The summary is, you should prefer functions from the Observable module. If it has everything you need, there is no need for Reactive Framework. If it does not, then you'll need either Reactive Framework or MiniRx (which, I believe, is sometimes more efficient too).
The F# Event module dates back to 2006, so I think Reactive Framework is clearly inspired by that, but it does not fully replace the F# functionality (mainly because it is not standard part of .NET or F# core).
I am converting several modules based on OCaml to F# and ran into the OCaml ''Quotation.add'' compiler directive for a quotation expander.
A quotation expander is a function written in OCaml. A call to the
Camlp4 library function ''Quotation.add'' adds the quotation
expander.
Camlp4 - Tutorial, Ch. 3
After searching for both a quotation expander and the ''Quotation.add'' compiler directive in F# books, the F# site, Google and here, the answer I get is no.
Can someone confirm that F# does not support the concept of the OCaml ''Quotation.add'' compiler directive or the concept of OCaml quotation expanders.
EDIT
Note: I just learned that Camlp4 is a preprocessor-pretty-printer of OCaml, I thought it was a separate library for OCaml when I asked the question; now it makes sense.
You're correct - the camlp4 library is not available for F#, so I'm afraid you'll need to use another approach to re-implement the required functionality in F#.
In general, there are a couple of related technologies that you can use from F# (but it is hard to tell if any of them is useful for you without knowing more about the specific problem):
F# Quotations allow you to manipulate F# code, but have limited compilation capabilities (are good i.e. for translating F# to SQL, JavaScript or perhaps GPU)
F# CodeDom (from F# PowerPack) allows you to generate code and compile it using F# compiler, but you can either generate code as text or using .NET object-oriented style.
T4 Templating is a .NET templating mechanism, though last time I checked, it did not support F# (but I think the Mono version might work)
F# Type Providers (thanks to kvb) make it possible to generate types by F# plugin for a compiler. This handles cases where you're using camplp4 to generate some types to be used later (i.e. from a shorter specification), but they have only limited use if you need to generate some code
Just curious - are protocol buffers usable with F#? Any caveats etc.?
I'm just trying to answer this question myself.
Marc Gravell's protobuf-net project worked out-of-the-box with F# because it uses standard .NET idioms. You can use attributes to get serializing without having to write .proto files or do any two-stage compilation, or you can generate the necessary code from standard .proto files. Performance is good for .NET but a lot slower than alternatives like OCaml's built-in Marshal module. However, this library forces you to make every field in every message type mutable. This is really counter-productive because messages should be immutable. Also, the documentation leaves a lot to be desired but, then, this is free software.
I haven't managed to get Jon Skeet's protobuf-csharp-port library to work at all yet.
Ideally, you'd be able to serialize all of the built-in F# types (tuples, records, unions, lists, sets, maps, ...) to this wire format out-of-the-box but none of the existing open source solutions are capable of this. I'm also concerned by the complexity of these solutions: Jon Skeet's is 88,000 lines of C# code and comments (!).
As an aside, I am disappointed to see that Google protocol buffers do not specify standard formats for DateTime or decimal numbers.
I haven't looked at Proto# yet and cannot even find a download for Froto. There is also ProtoParser but it just parses .proto files and cannot actually serialize anything.
There isn't an F# specific one listed here, but there is an OCaml one, or there is a .NET "general" one (protobuf-net).
In all honesty, I simply haven't gotten around to trying protobuf-net with F# objects, in part because I simply don't know enough F#, but if you can create POCOs they should work. They would need to have some kind of mutability (perhaps even just private mutability) to work with protobuf-net, though.
If you are happy to generate a C# DTO and just consume that from F#, then protobuf-net or Jon's port should work just fine.
I'd expect both my own port and Marc Gravell's to work just fine with F#, to the same extent that any other .NET library does. In other words, neither port is written in a way which is likely to produce idiomatic F# code, but they should work.
My port will generate C# code, so you'll need to build that as a separate project for your serialization model - but that should interoperate with F# without any problems. The generated types are immutable (with mutable builders) so that should help in an F# context.
Of course, you could always take the core parts of either project and come up with an idiomatic F# solution too - whether you port the whole project to F# or use the existing libraries with an F# code generator and helper functions, or something like that.
I recently wrote a binding for a C library using SWIG. While a good deal of it was straight forward and used only basic SWIG functionality, I ran into trouble when I needed to support one function which took a C callback as an argument, which is not supported for SWIG. I solved this by writing Python-specific code to provide a custom callback in which I called the Python 'eval' function to evaluate a supplied Callable.
While this worked nicely, it was unfortunate for me.. I had been hoping to use SWIG to take advantage of its support for tens of languages, but now I'm stuck having to figure out callbacks in every single language I wish to support. This makes my binding work magnitudes less useful, as I now have to solve the same problem many times, manually--the opposite of the point of using SWIG.
Are there any tool like SWIG that also handles C callbacks?
It's a bit rounadabout but if you recompile the C project in C++ or create a C++ extension, then you can take advantage of virtual function overloading.
Most SWIG language module have support for directors which allow a class in the target language to derive from a class in the C++ library. This way any overridden virtual function act as a callback.
I use blogger and I install a windows live writer, I don't know how to insert F# code.
I installed several code highlighter, none of which support ocaml or f#.
I now use VSPaste, a plugin for WLW, which can 'paste from Visual Studio' and copies the VS colors.
Get a better syntax highlighter. Alternatively, store your code in Github Gists and embed on your blog. That way, any code that Github can highlight, you can highlight.
[BTW, the tool you use to write your blog has nothing to do with your syntax highlighting woes.]
You may want to post this question on HubFS. There's more likely to be people there who know what options are available.
I think it depends on how sophisticated syntax highlighting you need. However, most of the formatters should be customizable, so you should be able to adapt them to work reasonably well with F#. On my blog, I use this formatter for C# (which is based on regular expressions), and I added my definition for the F# langauge.
Since I typically use the // comment format in blog posts and strings are formatted in the same way as in C#, I didn't have to do many changes. I only added a couple of F# operators (mostly as I needed them). I also added F# keywords which you can find in the language specification.
Since F# is somehow based on ML -- Ocaml or ML highlighter can help in most cases (except for example "//" comments).