F# IKVM Type Provider

After reading Sergey Tihon’s post about IKVM and the stanford parser. I thought I’d try and create a IKVM type provider.

The first version can be found on my GitHub account here.
The following code shows a simple sample on how to use the type provider.

    let\[<Literal>\] jar = @"D:\Appdev\IKVM.TypeProvider\IKVM.TypeProvider.Tests\SimpleJar\out\artifacts\SimpleJar.jar"
    let\[<Literal>\] ikvmPath = @"D:\Appdev\IKVM.TypeProvider\IKVM\bin\"

    type Jar = IKVM.IKVMProvider<JarFile=jar, IKVMPath=ikvmPath>
    type HW = Jar.hello.HelloWorld

    \[<EntryPoint>\] 
    let main(args) =
        let hw = new HW("IKVM")
        printf "%s" (hw.Say("Hello from java, well .NET"))
        printf "%s" (hw.Close())
        System.Console.ReadLine() |> ignore
        0

The above is the same sample included in the source. As you can see it requires an IKVM distribution your machine and requires your consuming project to reference IKVM.OpenJDK.Core.

This is very early days, for this so I would be surprised if it works for anything even moderately complex. But I hope to change this to support everything that IKVM supports assuming I get the time, in my day job :). Feel free to have a play and let me know what works and what doesn’t.

The biggest gripe I have at the minute is that the type providers don’t allow direct references to generated types, hence HW representing the class I wish to use. I would prefer to just open the “namespace“ as a type and then use the classes from there. Like the following

    let\[<Literal>\] jar = @"D:\Appdev\IKVM.TypeProvider\IKVM.TypeProvider.Tests\SimpleJar\out\artifacts\SimpleJar.jar"
    let\[<Literal>\] ikvmPath = @"D:\Appdev\IKVM.TypeProvider\IKVM\bin\"

    type Jar = IKVM.IKVMProvider<JarFile=jar, IKVMPath=ikvmPath>
    type helloNS = Jar.hello

    \[<EntryPoint>\] 
    let main(args) =
        let hw = new helloNS.HelloWorld("IKVM")
        printf "%s" (hw.Say("Hello from java, well .NET"))
        printf "%s" (hw.Close())
        System.Console.ReadLine() |> ignore
        0

I suspect not being able to do this will become problematic for large assemblies. as you will have to create an indirect reference for each type that you used.

Advertisements
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

3 Responses to F# IKVM Type Provider

  1. Pingback: F# Weekly #9, 2013 | Sergey Tihon's Blog

  2. Pingback: The Promise of F# Language Type Providers « Inviting Epiphany

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s