interface

What is (in simple understanding) narrow contract and wide contract in terms of c++ interface(s)?

亡梦爱人 提交于 2019-12-21 00:49:33
问题 While walking through some c++11 concepts, I came across the terms narrow contract and wide contract . But I failed to figure out a simple function example(s) which is/are written for these contracts. Can I see a simple function example that distinguishes between these two contracts? 回答1: Wide contract functions have well-defined behavior for all possible inputs, while narrow contracts mean that the functions can only be called when certain preconditions are met. Note that input might include

Ruby - Platform independent way to determine IPs of all network interfaces?

老子叫甜甜 提交于 2019-12-20 23:29:19
问题 Is there an easy way in Ruby for me to get a list of the IP addresses for all network interfaces? It needs to work in Linux/Win/OSX and I'd prefer to not have to parse ifconfig/ipconfig unless I absolutely have to. 回答1: As of Ruby 2.1, Socket#getifaddrs is available: 001:0> require 'socket' => true 002:0> Socket.getifaddrs.map { |i| i.addr.ip_address if i.addr.ipv4? }.compact => ["127.0.0.1", "192.168.1.121", "192.168.1.181"] 回答2: Check out the following post: http://coderrr.wordpress.com

avoid implementation of a method which is there in interface - java

我的梦境 提交于 2019-12-20 21:21:12
问题 I have a interface like the below : public interface a { public void m1(); public void m2(); public void m3(); } public class A implements a { public void m3() { // implementation code } } I want to avoid implementation for the rest of the method. one way is to have all the methods without implementing in the class that tries to implement interface . How do I avoid this. Example code would help me understand better :) 回答1: public interface a{ public void m1(); public void m2(); public void m3

Is there a non-reference-counted base class like TInterfacedObject?

こ雲淡風輕ζ 提交于 2019-12-20 17:38:46
问题 I need a base class like TInterfacedObject but without reference counting (so a kind of TNonRefCountedInterfacedObject ). This actually is the nth time I need such a class and somehow I always end up writing (read: copy and pasting) my own again and again. I cannot believe that there is no "official" base class I can use. Is there a base class somewhere in the RTL implementing IInterface but without reference counting which I can derive my classes from? 回答1: In the unit Generics.Defaults

C#: Enums in Interfaces

那年仲夏 提交于 2019-12-20 17:38:32
问题 I've seen a couple similar threads to this question, but none of them really answer the question I want to ask. For starters, unfortunately I'm working with existing API code so sadly, while there may be a better way to do what I'm asking about, I'm locked in to doing it similarly to the way it is because backwards compatibility is non-negotiable. I have a response class that currently contains an enum for an error code and a string description. The error codes define a fairly nice and

Cannot type switch on non-interface value

雨燕双飞 提交于 2019-12-20 17:26:08
问题 I am playing with type assertion using the following dummy code, and I got the error: cannot type switch on non-interface value Does anyone know what does that mean? package main import "fmt" import "strconv" type Stringer interface { String() string } type Number struct { v int } func (number *Number) String() string { return strconv.Itoa(number.v) } func main() { n := &Number{1} switch v := n.(type) { case Stringer: fmt.Println("Stringer:", v) default: fmt.Println("Unknown") } } http://play

Cannot type switch on non-interface value

依然范特西╮ 提交于 2019-12-20 17:25:08
问题 I am playing with type assertion using the following dummy code, and I got the error: cannot type switch on non-interface value Does anyone know what does that mean? package main import "fmt" import "strconv" type Stringer interface { String() string } type Number struct { v int } func (number *Number) String() string { return strconv.Itoa(number.v) } func main() { n := &Number{1} switch v := n.(type) { case Stringer: fmt.Println("Stringer:", v) default: fmt.Println("Unknown") } } http://play

Difference between abstract class with all method abstract and interface?

时光怂恿深爱的人放手 提交于 2019-12-20 15:25:13
问题 I had an interview where interviewer asked me first what is the difference between abstract class with all the methods abstract and an interface. I replied that if it is required to inherit something in the future you will not be able to do it if you have already extended a class. Then, he stated that it was a situation where one would never have to extend any other class and you have to implement a contract. In this circumstance, which would be better, an abstract class or an interface? I

Should I define interfaces in Duck Typed languages?

元气小坏坏 提交于 2019-12-20 11:56:12
问题 I'm just about to write my first application in a duck typed language (Groovy). If I was to write the same application in a static typed language then I would need to define some interfaces. Obviously because of the duck typing in Groovy they are not actually required. At the moment I am thinking that it might make sense to define them anyway as documentation of the methods that need to be implemented in the various objects. Am I missing the point? 回答1: I've been reading on this recently here

Anonymous/inline interface implementation in TypeScript

隐身守侯 提交于 2019-12-20 11:48:11
问题 I've just started with TypeScript and I'm trying to understand why the following inline object definition isn't considered valid. I have a collection of objects - their type is irrelevant (to me), but they implement the interface so that when I iterate through them I know that the interface methods will be present in each object in the collection. I came across a "compiler" error when I tried to create an object with private information required to implement the required method: interface