Implementing an interface in f#
Here is my c# interface:
public interface ITemplateService
{
string RenderTemplate(object model, string templateName);
string RenderTemplate(object model, string templateName, string directory);
}
I'm trying to make an implementation in F# but got an error on the keyword end
. (unexpected end
in implementation file)
module TemplateService
open DotLiquid
type TemplateService =
inherit ITemplateService
member this.RenderTemplate model (templa开发者_如何学PythonteName:string):string = ""
member this.RenderTemplate model (templateName:string, directory:string):string = ""
end//error here.
ps. What is this code in F#:
Template template = Template.Parse(stringToTemplate);
template.Render(Hash.FromAnonymousObject(model));
Since you are implementing an interface you'll want to use this syntax.
type TemplateService() =
interface ITemplateService with
member this.RenderTemplate model (templateName:string):string = ""
member this.RenderTemplate model (templateName:string, directory:string):string = ""
In addition to ChaosPandion's answer:
A class can either be defined like this:
type ClassName(constructorArguments) =
class
...
end
or like this:
type ClassName(constructorArguments) =
...
So you need either both the class
and the end
keyword or none of them. Usually people use the form
without class
and end
.
Your other code snippet would look something like this:
let template = Template.Parse stringToTemplate
template.Render (Hash.FromAnonymousObject model)
精彩评论