duck-typing

Why Collection Initialization Throws NullReferenceException

荒凉一梦 提交于 2019-12-01 22:31:11
The following code throws a NullReferenceException : internal class Foo { public Collection<string> Items { get; set; } // or List<string> } class Program { static void Main(string[] args) { new Foo() { Items = { "foo" } // throws NullReferenceException }; } } Why don't collection initiliazers work in this case, although Collection<string> implements the Add() method, and why is NullReferenceException is thrown? Is it possible to get the collection initializer working, or is Items = new Collection<string>() { "foo" } the only correct way to initialize it? Sriram Sakthivel You never

In Scala, why can't I implement a trivial generic function like this?

随声附和 提交于 2019-12-01 21:19:46
I want a generic function called "double", which behaves like this and could be applied to any type with def +(x:T):T method: double("A") > "AA" double(1) > 2 double(0.2) > 0.4 So I write this function like this: def double[T](x:T):T = { x+x } But when I run it in REPL, scala compains about it: scala> def double[T](x:T):T = { x+x } <console>:7: error: type mismatch; found : T required: String def double[T](x:T):T = { x+x } ^ I think structural type may be an approach to implement duck typing, and I tried something like this, but it doesn't work either: def double[T <: { def +(x:T):T }](x:T):T

Does LINQ “Query Syntax” Support Duck Typing?

余生颓废 提交于 2019-12-01 14:13:08
Regarding LINQ query syntax... var foo = new List<int> { 1, 2 }; var boo = from n in foo where n > 1 select n; ...I always thought this syntax was limited to operating on IEnumerable . Or at least until I learned about IQueryable. And perhaps IObservable as well. But I recently noticed a suggestion that query syntax is based on duck typing . That story didn't look terribly convincing, until I found a site that is dedicated to LINQ to Tasks . LINQ to Tasks looks like it is wholly dependent on duck typing with query syntax! Ok, what is going on here? Is query syntax using duck typing or not?

What about memory layout means that []T cannot be converted to []interface in Go?

依然范特西╮ 提交于 2019-12-01 12:23:22
So I've been reading these two articles and this answer Cannot convert []string to []interface {} says that the memory layout needs to be changed. http://jordanorelli.com/post/32665860244/how-to-use-interfaces-in-go says that understanding the underlying memory makes answering this question easy, and http://research.swtch.com/interfaces , explains what is going on under the hood. But for the life of me I can't think of a reason, in terms of the implementation of interfaces as to why []T cannot be cast to []interface. So Why? VonC The article " InterfaceSlice " try to detail: A variable with

What about memory layout means that []T cannot be converted to []interface in Go?

大兔子大兔子 提交于 2019-12-01 09:55:28
问题 So I've been reading these two articles and this answer Cannot convert []string to []interface {} says that the memory layout needs to be changed. http://jordanorelli.com/post/32665860244/how-to-use-interfaces-in-go says that understanding the underlying memory makes answering this question easy, and http://research.swtch.com/interfaces, explains what is going on under the hood. But for the life of me I can't think of a reason, in terms of the implementation of interfaces as to why []T cannot

Autocompletion in dynamic language IDEs, specifically Python in PyDev

若如初见. 提交于 2019-12-01 06:23:24
I'm new to Python, with a background in statically typed languages including lots and lots of Java. I decided on PyDev in eclipse as an IDE after checking features/popularity etc. I was stunned that auto-complete doesn't seem to work properly for builtins. For example if I try automcomplete on datafile after: datafile = open(directory+"/"+account, 'r') datafile. No useful methods are suggested (e.g. realines). Only things like call . I am used to learning a language by jumping into class definitions and using lots of auto-complete to quickly view what a class will do. My PyDev 'interpreter' is

Autocompletion in dynamic language IDEs, specifically Python in PyDev

有些话、适合烂在心里 提交于 2019-12-01 05:31:25
问题 I'm new to Python, with a background in statically typed languages including lots and lots of Java. I decided on PyDev in eclipse as an IDE after checking features/popularity etc. I was stunned that auto-complete doesn't seem to work properly for builtins. For example if I try automcomplete on datafile after: datafile = open(directory+"/"+account, 'r') datafile. No useful methods are suggested (e.g. realines). Only things like call . I am used to learning a language by jumping into class

Duck typing and class methods (or, how to use a method from both a class and an instance?)

家住魔仙堡 提交于 2019-12-01 01:05:17
I have some code which I would like to pass instances or classes interchangeably. All I will do in that code is to call a method that I expect both classes and instances to have (the method go() in the example below). Unfortunately, I can't create a classmethod with the same name of a regular method... See example below. I initially expected the second call to produce an a instead of a b . Any advice on how to achieve this? Type "help", "copyright", "credits" or "license" for more information. >>> class A(object): ... def go(self): ... print "a" ... @classmethod ... def go(cls): ... print "b"

F# and duck-typing

为君一笑 提交于 2019-11-30 17:27:35
Let's say I defined in F# the following two types: type Dog = { DogName:string; Age:int } type Cat = { CatName:string; Age:int } I was expecting the following method to work for both cats and dogs: let isOld x = x.Age >= 65 Actually, what seems to happen is that isOld will only accept cats: let dog = { DogName = "Jackie"; Age = 4 } let cat = { CatName = "Micky"; Age = 80 } let isDogOld = isOld dog //error My hopes were that F# would be smart enough to define some kind of "virtual" interface X for both cats and dogs so that isOld would accept a X as argument, instead of a Cat . This isn't

Why interfaces must be declared in Java?

时光怂恿深爱的人放手 提交于 2019-11-30 15:11:16
问题 Sometimes we have several classes that have some methods with the same signature, but that don't correspond to a declared Java interface. For example, both JTextField and JButton (among several others in javax.swing.* ) have a method public void addActionListener(ActionListener l) Now, suppose I wish to do something with objects that have that method; then, I'd like to have an interface (or perhaps to define it myself), e.g. public interface CanAddActionListener { public void