interface

object expression and captured state in F#

放肆的年华 提交于 2019-12-10 14:56:33
问题 What makes the first implementation KO ? type IToto = abstract Toto : unit -> unit { new IToto with member this.Toto = fun () -> () } { new IToto with member this.Toto () = () } 回答1: In the compile representation, there is a difference between property of a function type, compiled as FSharpFunc<unit, unit> Toto { get; } , and a method taking unit and returning unit, compiled as unit Toto() . The first object expression implements a different interface: type IToto = abstract Toto : (unit ->

Call Go function that accepts a slice of interface A with a slice of struct B (B implements A)

匆匆过客 提交于 2019-12-10 14:54:49
问题 I have the following types: type Statement interface { Say() string } type Quote struct { quote string } func (p Quote) Say() string { return p.quote } func Replay(conversation []Statement) { for _, statement := range conversation { fmt.Println(statement.Say()) } } I think I have a fairly good grasp of why a function that accepts a parameter of type []Statement , cannot be called with []Quote ; even though Quote implements Statement , []Quote does not implement []Statement . []Statement is

MVC 3 passing entity as an Interface

不羁岁月 提交于 2019-12-10 14:44:42
问题 I'm currently working on an MVC 3 project using Ninject as my DI, the business objects are stored in a separate assembly. I'm running into an issue with the controller parameters, when posting back for CRUD operations I'm getting the error "Cannot create an instance of an interface". I am aware that you can't create an instance of an interface, but it seems like the only way I can get around this is to use a custom model binder and pass the FormCollection through. This seems really messy and

how List<T> does not implement Add(object value)?

岁酱吖の 提交于 2019-12-10 14:42:37
问题 I believe it's pretty stupid, and I am a bit embarrassed to ask this kind of question, but I still could not find the answer: I am looking at the class List<T> , which implemetns IList . public class List<T> : IList one of the methods included in Ilist is int Add(object value) I understand that List<T> should not expose that method (type safety...), and it really does not. But how can it be? mustnt class implement the entire interface? 回答1: I believe that this (interface) method is

In Go, can both a type and a pointer to a type implement an interface?

本秂侑毒 提交于 2019-12-10 14:41:45
问题 For example in the following example: type Food interface { Eat() bool } type vegetable_s struct { //some data } type Vegetable *vegetable_s type Salt struct { // some data } func (p Vegetable) Eat() bool { // some code } func (p Salt) Eat() bool { // some code } Do Vegetable and Salt both satisfy Food , even though one is a pointer and the other is directly a struct? 回答1: The answer is easy to get by compiling the code: prog.go:19: invalid receiver type Vegetable (Vegetable is a pointer type

How to define a fmap on a record structure with F#

巧了我就是萌 提交于 2019-12-10 14:21:34
问题 is there a possibility to create an fmap for records so that I can apply the same function to record fields of similar bur different types Let say I have a record field type Item and a record X and function transform type Item<'a, 'b> = Item of 'a * 'b let transform (i: Item<'a, 'b>) : Item<'a, string> = let (Item (x, y)) = i Item (x, sprintf "%A" y) type X<'a> = { y: Item<'a, int> z: Item<'a, bool> } with member inline this.fmap(f) = { y = f this.y z = f this.z } now the line z = f this.z

Force Singleton Pattern on a Class implementing an Interface

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 13:54:59
问题 I better explain the question with an example. I have an Interface Model which can be used to access data. There can be different implementations of Model which can represent the data in various format say XMl , txt format etc. Model is not concerned with the formats. Lets say one such implementation is myxmlModel . Now i want to force myxmlModel and every other implementation of Model to follow Singleton Pattern .The usual way is to make myxmlModels constructor private and provide a static

How should I ensure disposal of possibly disposable objects?

微笑、不失礼 提交于 2019-12-10 13:35:39
问题 I am working on a .NET project, which needs to interact with some user defined classes - reffered to as "jobs". All job classes must implement a specific interface IJob in order order for the library to consume them. Sometimes a job class might hold unmanaged resource, which needs to be explicitly disposed. How should I ensure that all jobs are properly disposed after use, if I do not know in advance if the job needs explicit disposal? I have a few ideas myself, but would like to hear your

Is there a recognized pattern for useless objects that implement an interface?

為{幸葍}努か 提交于 2019-12-10 13:27:42
问题 For example, (although it's not an interface) the Stream class in .NET has an implementation provided by Stream.Null which simply discards the data. PowerShell has Out-Null. In applications I've developed, I've often found it useful to implement an interface IFoo with a default implementation NullFoo or similar when it is preferable to have a useless implementation rather than not passing an object at all. My question is how should I refer to this practice in documenting or explaining an

How to auto-generate methods of the implemented interface

柔情痞子 提交于 2019-12-10 13:23:49
问题 Is there a way in PhpStorm to automatically generate the empty methods that are required by the interface the given class is implementing? Say we have an Interface with 3 methods - when defining the new class that implements this interface - some option to auto-generate containers for all required methods. 回答1: From my comment: You could try ALT + ENTER . This normaly shows up a small "dialog" to autogenerate phpdoc, functions, etc. 回答2: Sure, you can. There are 3 ways to do it : Press Ctrl +