dynamic-typing

When should I use type checking (if ever) in Python?

我只是一个虾纸丫 提交于 2019-12-23 10:19:14
问题 I'm starting to learn Python and as a primarily Java developer the biggest issue I am having is understanding when and when not to use type checking. Most people seem to be saying that Python code shouldn't need type checking, but there are many cases when I believe it is necessary. For example, let's say I need to use a method parameter to perform an arithmetic operation, why shouldn't I make sure the argument is a numeric data type? This issue is not only limited to functions. The same

Dynamic type cast from id to class in objective c

陌路散爱 提交于 2019-12-23 09:05:34
问题 I would like to cast dynamically in Objective C and access instance properties. Here a pseudo code: id obj; if (condition1) obj = (Class1*)[_fetchedResults objectAtIndex:indexPath.row]; else obj = (Class2*)[_fetchedResults objectAtIndex:indexPath.row]; NSNumber *latitude = obj.latitude; Then the compiler tells me the following: property 'latitude' not found on object of type '__strong id' Either Class1 and Class2 are core data entities and have nearly the same kind of attributes. In

Dynamic typing and return values in Objective-C

房东的猫 提交于 2019-12-18 06:54:30
问题 I have run into a very strange behaviour I can’t make sense of. I have a Texture class with contentWidth property of type int . This class is wrapped in a Image class that has a width property of type int . The width of an Image is computed simply as the contentWidth of the underlying texture: - (int) width { return texture.contentWidth; } Now the Image is used by a Button class (by composition, not inheritance) that wants to read the image size: // image is of type ‘id’ int width = [image

Which languages are dynamically typed and compiled (and which are statically typed and interpreted)?

自闭症网瘾萝莉.ら 提交于 2019-12-17 22:56:35
问题 In my reading on dynamic and static typing, I keep coming up against the assumption that statically typed languages are compiled, while dynamically typed languages are interpreted. I know that in general this is true, but I'm interested in the exceptions. I'd really like someone to not only give some examples of these exceptions, but try to explain why it was decided that these languages should work in this way. 回答1: Here's a list of a few interesting systems. It is not exhaustive!

Do union types actually exist in python?

巧了我就是萌 提交于 2019-12-17 19:35:06
问题 Since python is dynamically typed, of course we can do something like this: def f(x): return 2 if x else "s" But is the way python was actually intended to be used? or in other words, do union types exist in the sense they do in racket for example? Or do we only use them like this: def f(x): if x: return "x" where the only "union" we need is with None? 回答1: Union typing is only needed when you have a statically typed language, as you need to declare that an object can return one of multiple

why + sign is the exception in javascript?

帅比萌擦擦* 提交于 2019-12-12 14:43:16
问题 i did this > 5 + 2 // 7, this is correct > 5 - 2 // 3 , obviously > 5 - "2" // 3 , ohh, that's awesome > 5 % "2" // 1 , :) > 5 / "2" // 2.5,looks like 2 is automatically converted to integer.Perfect! > 5 + "2" // "52" Really? Certainty, something extra is going on with plus sign. What's that and why ? 回答1: As per the ECMA 5.1 Standard Specification for Binary + operator, 7. If Type(lprim) is String or Type(rprim) is String, then Return the String that is the result of concatenating ToString

Create an instance of a class from a string name in Haxe

孤街浪徒 提交于 2019-12-10 02:56:36
问题 Let's say i acquire the name of a class that i made as a String . How can i Instantiate the class with the name contained in that string? I I know it will be derived from a certain parent class, but the actual class will vary. 回答1: var instance : MyClass = Type.createInstance(Type.resolveClass("path.to.MyClass"), []); Few notes: resolveClass() takes the full path (packages included) of the classe you need createInstance() takes as the second argument an array of values that are applied to the

When is sqlite's manifest typing useful?

我只是一个虾纸丫 提交于 2019-12-10 00:55:15
问题 sqlite uses something that the authors call "Manifest Typing", which basically means that sqlite is dynamically typed: You can store a varchar value in a "int" column if you want to. This is an interesting design decision, but whenever I've used sqlite, I've used it like a standard RDMS and treated the types as if they were static. Indeed, I've never even wished for dynamically typed columns when designing databases in other systems. So, when is this feature useful? Has anybody found a good

Haskell — get TypeRep from concrete type instance

你离开我真会死。 提交于 2019-12-08 16:25:28
问题 I want to write a function with this type signature: getTypeRep :: Typeable a => t a -> TypeRep where the TypeRep will be the type representation for a , not for t a . That is, the compiler should automatically return the correct type representation at any call sites [to getTypeRep ], which will have concrete types for a . To add some context, I want to create a "Dynamic type" data type, with the twist that it will remember the top-level type, but not its parameter. For example, I want to

How to describe function arguments in dynamic typed languages?

て烟熏妆下的殇ゞ 提交于 2019-12-07 07:51:08
问题 My question is more oriented toward Python, but it may also be about JavaScript or other scripting languages. I usually develop with statically typed languages (Java, C++, ActionScript, ...). I like to use from time to time Python, and I also need to sometimes use JavaScript. These are dynamic typed languages. Nothing wrong with that, but I usually have lots of headaches to understand which parameters are needed in a function or in a method. It happens even if it is my own code with some