2

As far as I know, both tools are used for code generation. Libraries generated with t4 can be used everywhere and type providers can only be used with F#, so what is the point?

Chris Ballance
  • 33,810
  • 26
  • 104
  • 151
dotneter
  • 1,689
  • 2
  • 15
  • 24
  • Your last comment is not true. You can consume F# Type Providers from C#; see http://stackoverflow.com/questions/12118866/how-do-i-create-an-f-type-provider-that-can-be-used-from-c – pad Sep 08 '12 at 09:03

3 Answers3

6

T4 is strictly code gen., whereas F# supports both code gen. and erased types, the latter being a feature unique to F# type providers.

Moreover, T4 is pretty clunky in my experience. I've used a number of them in the past year or so in C# projects, and while very useful, there are a number of issues around source control and build integration I've encountered. I've honestly not much experience with F# type providers, but from what I've seen, it is cleaner than T4 even for code gen. scenarios (specifically in regard to build integration).

Stephen Swensen
  • 22,107
  • 9
  • 81
  • 136
5

As @pad said, it is possible to consume F# Type Providers from C# (depending on how you implement your type provider).

IMO, the main benefit of type providers over T4 is that they're distributed as .NET assemblies, which makes it much easier/simpler to use them from multiple projects. AFAIK, T4 requires you to copy (or otherwise include) the macro files into each project you want to use them in. (It's been a few years since I've used T4 though, so it's possible this has changed.)

In general I think that there is a large overlap in functionality between Type Providers and T4; so yes, if you wanted, you could replicate much of the Type Provider functionality with T4. However, the more functional programming you do, the more you'll learn that modularity -- in just the right amounts -- is what makes functional languages much more productive than their imperative bretheren; and for this same reason, the way Type Providers are constructed and the way they integrate with the F# compiler make them better to use in practice than T4 macros.

Jack P.
  • 11,487
  • 1
  • 29
  • 34
3

I have very limited experience with TypeProviders but I've used T4 extensively for 4 years

From what I've read from TypeProviders these are my reasons for sticking with T4:

  1. T4 is conceptually simple, ie PHP/ASP that generates code
  2. T4 generates readable code which is compiled and debugged like normal source code
  3. If future maintainers would like to they can stop using T4 and maintain the generated artifacts manually (I would of course not recommend that but it is possible)
  4. T4 can generate C#, VB, F#, C++, C, T-SQL, XML and so on. I mainly used it for C# but generating C++ and T-SQL has proved to be big time-saves.
  5. T4 and partial works very well together

Code sharing in T4 is done using <#@include#> and that concept works well for T4.

To me what makes T4 great is the simple concept, the great integration with Visual Studio and the versatility (as it can generate any kind of text).

Before I did MetaProgramming with System.Linq.Expression, System.Reflection.Emit, C++ partial template tricks, C++ preprocessor higher-order macros and self-generating assembly code for the 68000. All of these techniques are cool when writing but not cool for the maintainers. They were also hard to debug and generated poor error messages.

To me TypeProviders seems harder to maintain and the code harder to debug but then the little I know about TypeProviders comes from reading some blogs.

All in my humble opinion of course.