duck-typing

What's the relationship between C++ “concept” and duck typing?

风流意气都作罢 提交于 2021-02-18 22:10:28
问题 There was an earlier question (8 years ago!) about the relationship between templates and duck typing here: What's the relationship between C++ template and duck typing? I've borrowed and modified the tag line for my question on a new feature of C++. With C++20 there will be the new feature of "concept" that looks much more like a duck-typing feature. Is it correct that the new C++ "concept" is equivalent to duck typing for C++? If not, how is it different? 回答1: With C++20 there will be the

Static duck typing in C++

眉间皱痕 提交于 2021-02-07 02:39:56
问题 C++ has some sort of duck typing for types given by template parameters. We have no idea what type DUCK1 and DUCK2 will be, but as long as they can quack() , it will compile and run: template <class DUCK1, class DUCK2> void let_them_quack(DUCK1* donald, DUCK2* daisy){ donald->quack(); daisy->quack(); } But it's a bit inconvenient to write. When I do absolutely not care what actual types DUCK1 and DUCK2 are but rather want to fully use the idea of duck typing, then I would like to have

About “eval is evil” and “consenting adults” in Python [closed]

巧了我就是萌 提交于 2021-02-07 00:53:47
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 6 years ago . Improve this question I see many saying "eval is evil/dangerous/insecure", because one can do things like: eval("os.system('rm -rf /')") while in other posts, pythoner are considered as " consenting adults ", you don't have to do type checking because of python is of style

Why duck typing is allowed for classes in TypeScript

ぃ、小莉子 提交于 2021-01-28 14:09:56
问题 Looks like in TypeScript it's absolutely fine (from the compiler perspective) to have such code: class Vehicle { public run(): void { console.log('Vehicle.run'); } } class Task { public run(): void { console.log('Task.run'); } } function runTask(t: Task) { t.run(); } runTask(new Task()); runTask(new Vehicle()); But at the same time I would expect a compilation error , because Vehicle and Task don't have anything in common. And sane usages can be implemented via explicit interface definition:

Dynamic checking of type hints in Python 3.5+ [duplicate]

◇◆丶佛笑我妖孽 提交于 2020-08-25 06:30:56
问题 This question already has answers here : How to use type hints in python 3.6? (3 answers) Closed 13 days ago . The typing module implements type hints in Python 3.5+. However, this is not enforced, it seems to currently only exist for the benefit of static type checkers such as mypy and PyCharm. I was hoping it would be a viable alternative to duck typing. Question : Is there a way to turn on dynamic type checking in Python 3.7+ that I didn't find in Google search? So for example, if I define

Duck typing / dynamic proxies on existing instances of objects

拟墨画扇 提交于 2020-04-11 07:43:14
问题 I have an object handed into our library and passed through various processes. I need to attach some additional information to these objects as they pass through various stages and out the other end - a kind of dynamic decorator pattern, I guess, except adding additional properties rather than changing existing behaviour. I was hoping to use LinFu or Castle to create a dynamic proxy and implement an additional interface on the object to store this. Components that know about the extended

Indexable type - TypeScript

谁说我不能喝 提交于 2020-01-13 05:52:06
问题 Below syntax, interface StringArray { [index: number]: string; } states that when a StringArray is indexed with a number , it will return a string . For example - let myArray: StringArray;myArray = ["Bob", "Fred"];let myStr: string = myArray[0]; So, myArray is type constrained on values stored as string type, by declaring it with type StringArray . Key(index) is always string type, under the hood(JavaScript), despite mentioning it as number From the below syntax, class Animal{ name: string; }