Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to call F# Extension in VB.Net

Solved: IntelliSense just doesn't show the Extension!

Lets say we got the following extension method in F#:

[<Extension>]
module Extension =
    [<Extension>]
    let Increment(value : System.Int32) = value + 1

In C# I can call it like this way:

x.Increment(); //Result x=1

But the equivalent VB code to this returns an error, it doesn't find the extension method for the type integer:

x.Increment() 'No method called "Increment()" for type Int32

While it's possible to call the method by the standard way:

Increment(x) 'Works

So where is the difference between handling VB and C# on calling F# code, why isn't the VB environment able to resolve the extension methods?

like image 530
Tim Krüger Avatar asked Oct 04 '22 19:10

Tim Krüger


1 Answers

If you build a regular F# library project

namespace A
open System.Runtime.CompilerServices
[<Extension>]
module Extension =
    [<Extension>]
    let Increment(value : System.Int32) = value + 1

and then refer to this library from VB project

Imports A.Extension
Module Module1
    Sub Main()
        Console.WriteLine((1).Increment())
    End Sub
End Module

then VB treats F# extension method as expected <Extension>Public Function Increment() As Integer and works correctly, giving 2 as output.

A clean experiment does not indicate any VB-specific idiosyncrasy to F#-defined extension methods.

like image 168
Gene Belitski Avatar answered Oct 07 '22 18:10

Gene Belitski