Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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: "Incomplete 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?

like image 352
Anton Andreev Avatar asked Dec 29 '10 17:12

Anton Andreev


People also ask

How was C implemented?

The origin of C is closely tied to the development of the Unix operating system, originally implemented in assembly language on a PDP-7 by Dennis Ritchie and Ken Thompson, incorporating several ideas from colleagues. Eventually, they decided to port the operating system to a PDP-11.

What is implementing in C?

Basically, an implementation is the combination of a compiler and the C library it supports. – Jonathan Leffler. Apr 15, 2019 at 14:20. 4. C language = what you type into a text editor; implementation = the thing that actually does something with what you typed.


2 Answers

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

like image 196
Stephen Swensen Avatar answered Oct 09 '22 05:10

Stephen Swensen


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#

like image 20
Anton Andreev Avatar answered Oct 09 '22 03:10

Anton Andreev