Questions tagged [duck-typing]
The duck-typing tag has no usage guidance.
22
questions
3
votes
5
answers
430
views
Converting Dynamic Typing To Static Programatically
See: Type inference with duck typing - does this work? Why is it not used?
And: General approach for proving decidability/undecidability
Hello, I wanted to ask a theoretical question about type system ...
2
votes
4
answers
307
views
"Duck-type-y" interface/implementation
I do not know the "correct" name of that pattern, so I'd like to describe it with a simple example.
In C#, System.Windows.Window contains a ShowDialog method. I can define an interface containing ...
-1
votes
1
answer
348
views
The pythonic way: replacing interfaces with ducktyping vs inheritence
tldr: I have consumer-like classes that require a number of pieces of information to do their job. It's an "all or nothing" kind of thing: the "producers" providing them with data need to provide all ...
3
votes
1
answer
197
views
Efficient algorithm for deducing object type dynamically based on members
I am designing a DSL (in clojure, specifically; though this question is more general than that) in which 'entities' are tracked as immutable hashes/maps, and where the 'concept' membership of an ...
4
votes
5
answers
2k
views
Type inference with duck typing - does this work? Why is it not used?
Suppose we have a functional language where objects don't have explicitly defined types, but where named properties can nonetheless be accessed on objects. Is it then possible for the compiler to ...
3
votes
2
answers
2k
views
From a software development lifecycle perspective, is duck-typing a benefit or a problem?
Statically-typed languages such as Java afford the benefit of compile-time checking of types - you are guaranteed that an object is of a given type, so:
there is no need to spend time and resources ...
12
votes
3
answers
2k
views
How do you use duck typing in javascript without always checking for properties and methods?
I know javascript uses duck typing and at first I thought this would make polymorphism easy compared to strongly typed languages like C#. But now my functions that take arguments are littered with ...
5
votes
2
answers
2k
views
Does this code follow duck typing?
The principle of duck typing says that you shouldn't care what type of object you have - just whether or not you can do the required action with your object. For this reason the isinstance keyword is ...
23
votes
4
answers
2k
views
Why would many duck-typed dynamic programming languages use a class-based approach instead of prototype-based OOP?
Since quite many dynamic programming languages have the feature of duck typing, and they can also open up and modify class or instance methods at anytime (like Ruby and Python), then…
Question 1)
...
27
votes
2
answers
6k
views
Is there a difference between duck typing and structural typing? [duplicate]
Duck typing is deciding on the type of an object, based on the operations it supports and the attributes it owns. Structural typing is... the same thing?
What exactly is the difference?
13
votes
3
answers
6k
views
Is it possible to have a dynamically typed language without duck typing? [closed]
This question has been asked here, but received poor answers and didn't clarify the issue. I believe it justifies asking it again.
I understand that you can have duck typing with either dynamically ...
2
votes
2
answers
4k
views
Is there a way to prevent variables from changing their type in Python?
It is useful to have the interpreter derive the type of a variable automatically. This on its own is similar to the auto keyword in C++11. However, in Python variables can change their type after ...
6
votes
7
answers
5k
views
How can a statically typed language support duck typing?
I understand what dynamic and static type systems are, and what duck typing is. But I don't understand how you can have a static language that supports duck typing. To my understanding only a ...
6
votes
2
answers
684
views
Should duck typing affect naming conventions?
I'm a C++/C# developer and recently I've started to learn Python. I think I understand the concept of duck typing right now except for one thing.
When I'm coding in C++ or C# I sometimes create ...
11
votes
1
answer
5k
views
Duck typing, data validation and assertive programming in Python
About duck typing:
Duck typing is aided by habitually not testing for the type of arguments in method and function bodies, relying on documentation, clear code and testing to ensure correct use.
...
2
votes
1
answer
594
views
How should I distinguish between built-in types in Python?
I have a function that accepts either a string or a dictionary as an argument. That is, it can be called as either:
lookup_person({name: "John Smith", age: 57})
or
lookup_person("John Smith")
My ...
2
votes
1
answer
10k
views
How does Django turn field name strings to field name variables?
I'm very new to python and (coming from Java) am trying to think in a "pythonic" way. I'm having trouble understanding how Django turns a function (or variable) name given in string to the actual ...
51
votes
4
answers
9k
views
Python Forgiveness vs. Permission and Duck Typing
In Python, I often hear that it is better to "beg forgiveness" (exception catching) instead of "ask permission" (type/condition checking). In regards to enforcing duck typing in Python, is this
try:
...
11
votes
1
answer
9k
views
Implicit vs explicit interfaces
I think I understand the actual limitations of compile-time polymorphism and run-time polymorphism. But what are the conceptual differences between explicit interfaces (run-time polymorphism. ie ...
5
votes
5
answers
2k
views
Are design patterns independent of programming languages?
I have been recently working on Objective C and came across use of Delegate pattern.
I had seen most of the common patterns theoretically in Java, thanks to the Head First book.
But at times looking ...
18
votes
6
answers
7k
views
Is duck typing a subset of polymorphism
From Polymorphism on WIkipedia
In computer science, polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface.
From duck ...
34
votes
11
answers
21k
views
Why does PHP have interfaces?
I noticed that as of PHP5, interfaces have been added to the language. However, since PHP is so loosely typed, it seems that most of the benefits of using interfaces is lost. Why is this included in ...