天道酬勤,学无止境

dynamic-typing

How is it possible to dynamically cast to a Type that is named in a string with Swift 2.0?

Question I need to cast a return value to a specific type that I need to keep dynamic, like let cellType = "CellTypeToBeResolved" cell = (tableView.dequeueReusableCellWithIdentifier("myID") as? CellTypeToBeResolved)! How is this possible in Swift 2.0? Thnx! Answer1 You can't do it, because Swift is (deliberately) missing not one but two pieces of the puzzle: You can't turn a string into a class. More important, you can't cast down to a class expressed as a variable. The class to cast down to must be a literal class, i.e. it must be known at compile time. The compiler needs to know that this

2022-05-12 11:44:04    分类:技术分享    ios   casting   swift2   dynamic-typing

Where do you benefit from dynamic typing?

Question How often do you take advantage of dynamic typing in a way that really wouldn't be feasible in a statically typed language? What I'm interested in is, how often these are used within real world (rather than demonstration) code? Answer1 In theory, you can essentially "simulate" dynamic typing inside most statically typed languages, by encoding values in some kind of tagged union and making all operations behave correctly based on the input "types" (actually the input values). Of course, you then have to introduce a bunch of runtime error behaviours that occur when you encounter

2022-05-12 02:58:03    分类:技术分享    dynamic-typing

Inferred Type and Dynamic typing

Question In programming language what is the difference between Inferred Type and Dynamic typing? I know about Dynamic typing but don't get how dynamic typing is differ from Inferred Type and how? Could someone please provide explanation with some example? Answer1 Inferred type = set ONCE and at compile time. Actually the inferred part is only a time saver in that you don't have to type the Typename IF the compiler can figure it out. Type Inference is often used in conjunction static typing (as is the case with swift) (http://en.wikipedia.org/wiki/Type_inference) Dynamic type = no fixed Type -

2022-04-16 06:16:47    分类:技术分享    type-inference   dynamic-typing

type of object references in ruby

Question I am new to Ruby and currently trying a few examples from the Ruby book I am using as a guide: class Account attr_accessor :balance def initialize(balance) @balance = balance end end class Transaction def initialize(account_a, account_b) @account_a = account_a @account_b = account_b end def debit(account,amount) account.balance -= amount end def credit(account,amount) account.balance += amount end def transfer(amount) debit(@account_a, amount) credit(@account_b, amount) end end savings = Account.new(100) checking = Account.new(200) trans = Transaction.new(checking, savings) trans

2022-04-08 07:49:46    分类:技术分享    java   ruby   variables   dynamic-typing

How to deal with Python ~ static typing? [closed]

Question As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago. I am from Java world and I wonder what is so great about dynamic typing in Python besides missing errors while compiling the code? Do you like Python's typing? Do you have an example where it helped in a big project? Isn't it a

2022-03-08 01:58:54    分类:技术分享    java   python   static-typing   dynamic-typing

sorted-map throws exception on failed key look-up

Question user=> (def m (sorted-map 1 2)) #'user/m user=> (map? m) true user=> (get m :type) ClassCastException java.lang.Long cannot be cast to clojure.lang.Keyword clojure.lang.Keyword.compareTo (Keyword.java:114) It appears that sorted-map has chosen a numerical comparison function, which won't compare with a keyword. It would be nice to reason, "This thing supports IPersistentMap. So, I can call get on it to find out if it's a kind of map that I know about without risk of throwing an exception." The docstring for get says "Returns the value mapped to key, not-found or nil if key not present

2021-12-13 02:58:10    分类:技术分享    exception   dictionary   clojure   dynamic-typing

Create typings dynamically from object

Question Say I have the following two types: export type CollectionNames = 'twitter:tweets' | 'twitter:users' | 'twitter:metadata-cashtag' export type CollectionType<T extends CollectionNames> = T extends 'twitter:tweets' ? Tweet : T extends 'twitter:users' ? User : T extends 'twitter:metadata-cashtag' ? CashtagMetadataDb : never I feel this is very clunky and I'm not very keen on having the strings twice. Also it's possible to legally misspell them in the latter type. Is there any way to create these dynamically from an object such as this: typings = { 'twitter:tweets': Tweet, 'twitter:users'

2021-10-23 17:05:36    分类:技术分享    typescript   dynamic-typing   conditional-types

Swift 4: type(of: self) differs when using private/fileprivate

Question I implemented an extension to NSObject to get the dynamic type of my objects: extension NSObject { var dynamic_type : String { get { return String(describing: type(of: self)) } } } This works perfectly for public classes. In a class called InitialState dynamic_type would be "InitialState" (this is what I want) But as soon as I change the class to private or fileprivate it is something like "(InitialState in _AF5C6D4A3B423A6F0735A7740F802E5A)" (the parenthesis are also returned) Why is this and what does the part after "in" mean? How can I get the plain class name for every type of

2021-10-21 04:54:04    分类:技术分享    swift   private   swift-extensions   dynamic-typing

Is Python type safe?

Question According to Wikipedia Computer scientists consider a language "type-safe" if it does not allow operations or conversions that violate the rules of the type system. Since Python runtime checks ensure that type system rules are satisfied, we should consider Python a type safe language. The same point is made by Jason Orendorff and Jim Blandy in Programming Rust: Note that being type safe is independent of whether a language checks types at compile time or at run time: C checks at compile time, and is not type safe; Python checks at runtime, and is type safe. Both separate notion of

2021-10-19 03:03:42    分类:技术分享    python   types   type-safety   dynamic-typing   memory-safety

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

Question 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 thought process occurs to me for class variables. Why and when should I or shouldn't I use

2021-10-09 02:48:02    分类:技术分享    python   dynamic-typing