天道酬勤,学无止境

dynamic-typing

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

问题 我需要将返回值转换为需要保持动态的特定类型,例如 let cellType = "CellTypeToBeResolved" cell = (tableView.dequeueReusableCellWithIdentifier("myID") as? CellTypeToBeResolved)! 这在 Swift 2.0 中怎么可能? 谢谢! 回答1 你不能这样做,因为 Swift (故意)错过的不是一块而是两块: 你不能把一个字符串变成一个类。 更重要的是,您不能转换为表示为变量的类。 要转换为的类必须是文字类,即它必须在编译时已知。 编译器需要知道这个转换是合法的,而且它需要在编译时知道如何处理这个变量。 如果您希望能够将 MyCoolTableCell 实例消息发送到此单元格,则需要在代码中使用字面值 MyCoolTableCell 名称——不是字符串,也不是包含类型的变量,而是类型本身。 回答2

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

Where do you benefit from dynamic typing?

问题 您多久以在静态类型语言中实际上不可行的方式利用动态类型? 我感兴趣的是,这些在现实世界(而不是演示)代码中使用的频率如何? 回答1 理论上,您基本上可以在大多数静态类型语言中“模拟”动态类型,方法是在某种标记联合中编码值,并使所有操作根据输入“类型”(实际上是输入值)正确运行。 当然,你必须引入一堆运行时错误行为,当你遇到对这些值的无意义操作时会发生这些错误行为。 大多数非常非常难做的事情要么是有风险的,要么是不正确的。 由于反射的更自然编码,无需实例化高阶类型,动态类型语言中的许多大规模元编程要容易得多。 但是,要回答您的问题,我认为动态类型没有任何方式提供在合理的静态类型语言中不可行的功能。 回答2 恕我直言,当涉及到值时,动态类型在最好的情况下是“Meh”,在最坏的情况下会导致更多错误。 换句话说,就价值观而言,我没有发现任何好处。 真正的价值来自动态类型语言提供的元编程潜力。 没有像 Grails 这样的框架是不可能的。 grails 所做的一个例子是:如果你有一个域类,向它添加一个变量,然后框架会自动为你在域类上放置方法“findByYourVar”,在运行时可用。 因此,所有用 Java(或其他)编写繁琐的常见持久性方法都由框架提供给您。 回答3 老实说:动态语言(注意我说的是“动态语言”,而不是“动态类型语言”)提供的大多数好处与动态类型本身没有任何关系

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

Where do you benefit from dynamic typing?

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?

2022-04-30 03:25:19    分类:问答    dynamic-typing

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

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!

2022-04-27 14:57:41    分类:问答    ios   casting   swift2   dynamic-typing

推断类型和动态类型(Inferred Type and Dynamic typing)

问题 在编程语言中,推断类型和动态类型有什么区别? 我知道动态类型,但不知道动态类型与推断类型有何不同以及如何? 有人可以用一些例子来解释吗? 回答1

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

How can I type-check variables in Python?

I have a Python function that takes a numeric argument that must be an integer in order for it behave correctly. What is the preferred way of verifying this in Python? My first reaction is to do something like this: def isInteger(n): return int(n) == n But I can't help thinking that this is 1) expensive 2) ugly and 3) subject to the tender mercies of machine epsilon. Does Python provide any native means of type checking variables? Or is this considered to be a violation of the language's dynamically typed design? EDIT: since a number of people have asked - the application in question works

2022-03-26 08:28:38    分类:问答    python   typing   dynamic-typing

Inferred Type and Dynamic typing

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?

2022-03-18 12:41:09    分类:问答    type-inference   dynamic-typing

type of object references in ruby

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.transfer(60)

2022-03-14 12:24:07    分类:问答    java   ruby   variables   dynamic-typing