views:

252

answers:

3

Hi ;)!

I'm diving into F# and it's very fascinating. I'm trying to marry Optional Types and C# like here. Pretty interesting thing... however I miss something important I guess:

#light
namespace MyFSharp

// C# way
[<System.Runtime.CompilerServices.Extension>]
module ExtensionMethods =
    [<System.Runtime.CompilerServices.Extension>]
    let Great(s : System.String) = "Great"


using System;
using MyFSharp;  // reference the F# dll
class Program
{
    static void Main(string[] args)
    {
        var s = "foo";
        //s.Awesome(); // no
        Console.WriteLine(s.Great());  // yes
    }
}

That's very simple - and I guess it's too early for me or something... I get:

The type 'Extension' is not defined

Maybe it's too early... but I just don't get why it isn't found.

Thanks, Marius

+6  A: 

Does your F# project have a reference to the System.Core assembly?

EDIT: Hmm... perhaps explicitly call it ExtensionAttribute instead of just Extension? Maybe if the namespace is specified (instead of just a simple name) it doesn't try with the Attribute suffix?

Jon Skeet
Yes, it has. By default afaik VS adds this ref
wishi
A: 

Sounds like your F# project is targeting the .Net Framework 2.0.

Try changing it to .Net Framework 3.5 (under the project properties -> Application).

Johan Kullbom
+3  A: 

Extension methods need to be static in a static class. Does F# actually emit it as such? If not, then you are out of luck.

Update

I just tried it, and it appears to emit static class and method. I got the same error as you, but fixed by compiling as:

fsc -r System.Core test.fs

Then looking at the output of the assembly in Reflector, it looks all good and should work.

leppie