How to implement a C# interface in F#?
I would like to implement the following C# interface in F#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mono.Addins;
[TypeExtensionPoint]
public interface ISparqlCommand
{
string Name { get; }
object Run(Dictionary<string, string> NamespacesDictionary, org.openrdf.repository.Repository repository, params object[] argsRest);
}
This is what I have tried, but it gives me: "Incomple开发者_如何学Cte structured construct at or before this point in expression"
#light
module Module1
open System
open System.Collections.Generic;
type MyClass() =
interface ISparqlCommand with
member this.Name =
"Finding the path between two tops in the Graph"
member this.Run(NamespacesDictionary, repository, argsRest) =
new System.Object
What am I doing wrong? Maybe indentation is wrong?
I verified @Mark's answer in the comments. Given the following C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace org.openrdf.repository {
public class Repository {
}
}
namespace CSLib
{
[System.AttributeUsage(System.AttributeTargets.Interface)]
public class TypeExtensionPoint : System.Attribute
{
public TypeExtensionPoint()
{
}
}
[TypeExtensionPoint]
public interface ISparqlCommand
{
string Name { get; }
object Run(Dictionary<string, string> NamespacesDictionary, org.openrdf.repository.Repository repository, params object[] argsRest);
}
}
The following F# implementation (only change is adding ()
at the object construction) works "fine":
#light
module Module1
open System
open System.Collections.Generic;
open CSLib
type MyClass() =
interface ISparqlCommand with
member this.Name =
"Finding the path between two tops in the Graph"
member this.Run(NamespacesDictionary, repository, argsRest) =
new System.Object()
Though you don't need to use #light
anymore (it is the default) and you may want to head the NamespaceDictionary
paramater name warning that "Uppercase variable identifiers should not generally be used in patterns, and may indicate a misspelt pattern name." Also note that you will need to cast MyClass
to ISparqlCommand
in order to access the implemented members (not a question you asked, but easy to get confused by coming from C#): e.g (MyClass() :> ISparqlCommand).Name
Thanks to everyone! The following code actually works:
namespace MyNamespace
open System
open System.Collections.Generic;
open Mono.Addins
[<assembly:Addin>]
do()
[<assembly:AddinDependency("TextEditor", "1.0")>]
do()
[<Extension>]
type MyClass() =
interface ISparqlCommand with
member this.Name
with get() =
"Finding the path between two tops in a Graph"
member this.Run(NamespacesDictionary, repository, argsRest) =
new System.Object()
It is also an example of how to use Mono.Addins with F#
精彩评论