问题标签 [dynamic-typing]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c++ - 在给定指向其基类的指针的情况下识别子类?
假设我有一个抽象基类 Parent 和子类 Child1 和 Child2。如果我有一个接受 Parent* 的函数,有没有办法(可能使用 RTTI?)在运行时确定该函数实际收到的是 Child1* 还是 Child2*?
到目前为止,我在这里使用 RTTI 的经验是,当 foo 是 Parent* 时,typeid(foo) 返回 typeid(Parent*) 而不管 foo 属于哪个子类。
objective-c - 目标c中从id到类的动态类型转换
我想在 Objective C 中动态转换并访问实例属性。这是一个伪代码:
然后编译器告诉我以下内容: 在“__strong id”类型的对象上找不到属性“纬度”
Class1 和 Class2 都是核心数据实体,具有几乎相同的属性。在条件1 中, _fetchedResults返回Class1 类型的对象,在条件2 中, _fetchedResults返回 Class2 类型的对象。
有人可以给我一个提示如何解决这种问题吗?
谢谢!
types - 动态类型语言中的类型
动态类型语言(即 Python)在运行时执行类型检查,因此变量或参数可以引用任何类型的值。语言定义是否要求不键入变量和参数?修改语言以使变量和参数具有类型是否有意义?
asp.net-mvc - What's a best practice for using dynamic type in ASP.NET MVC 4?
As we know Microsoft introduced the dynamic type a long time ago. And I also applied it in some case in the ASP.NET MVC application. But to me, it is not good for all cases. In specific, it's seen to be violating some basic principals like The Acyclic Dependencies Principle. For example, I have a package A that using package B, then in B I use dynamic and reference to A. It work fine. So the question is how do I use the dynamic type in correct way?
algorithm - 动态类型语言中的快速属性查找?
我目前正在开发一种动态类型的语言。
我在开发过程中面临的主要问题之一是如何进行快速的运行时符号查找。
对于一般的、自由的全局和局部符号,我只需对它们进行索引,并让每个范围(全局或局部)保留一个符号数组并使用索引快速查找它们。我对这种方法非常满意。
但是,对于对象中的属性,问题要困难得多。我不能对它们使用相同的索引方案,因为我不知道我当前正在访问哪个对象,因此我不知道要使用哪个索引!
这是 python 中的一个示例,它反映了我希望用我的语言工作的内容:
有谁知道快速查找的任何巧妙技巧?我知道哈希图和展开树,所以如果有任何方法可以像我的其他查找一样高效,我会很有趣。
python - 检查 Python 中不需要的类型更改
我来自静态类型编程,我有兴趣了解动态类型编程背后的基本原理,以检查动态类型语言是否能更好地满足我的需求。
我读过鸭子编程背后的理论。我还读到单元测试(可取并用于静态类型编程)成为缺少编译时检查的动态语言的需要。
但是,我仍然害怕错过大局。特别是,如何检查变量类型意外更改的错误?
让我们用 Python 做一个非常简单的例子:
Pylint、pychecker 和 pyflakes 不会就这个问题发出警告。
处理这种错误的 Pythonic 方式是什么?
代码应该用 try/catch 包装吗?
language-design - 泛型类型、编译器和动态语言
在公开招标中有以下声明:
“在面向对象编程中使用泛型类型有助于在编译时进行类型检查。”
这是真的还是假的?我认为这是错误的,因为类型检查编译器没有改进,这与面向对象无关,而是与语言类型(弱类型或强类型)有关。有人请帮我解释为什么这个说法是错误的。
python - 如何动态知道python(django模板)中的类型是字典还是列表?
我只有 django python 代码,用于过滤器模板
我在 html 文件中调用:
他们 dict 是字典对象。但是当我运行它时。它向我显示错误:
我认为问题在于模板中的 dict 对象被视为列表,因此它是不可迭代的。如何让python知道对象是字典而不是列表?
python - 是否强制对 python 进行类型检查
我在这里问了这个问题,并被告知要在这里发帖。
我最近被困在一个叫做 java 的常规中后转向了 python。
但也许作为我使用强类型语言的剩余时间,我经常发现自己编写函数然后强制进行类型检查。例如:
我应该继续这样做吗?这样做/不这样做有什么好处?
我知道 assert 语句增加了函数调用的数量。让我们假设这可以忽略不计。在语义上我可以做些什么来强制类型检查?try/except 语句将如何帮助或伤害?
class - How to deal with polymorphism inside a class
In languages with dynamic typing, the use of polymorphism may trigger errors on a super-class.
I will try to explain my question with a simple example: Supposing a language with dynamic typing (like ECMAScript) and the following class structure:
In this very simple example, when i create an object of the class B, B::foo() call the parent A::foo(), which call "update". The object is an instance of B, so the "update" functions called is B::update, after that, in B::foo, the update function is again called (B::update). The final result is that A::update is never called, and idA still 0.
The class A work correctly when used alone, but after to extend it with B, the function foo() fail.
What is the correct solution this problem:
1) Force the class A to call A::update , that mean an ugly code every call to his own function (protect the super-class):
2) B::update is an extension of A::update, so B::update must call itself the parent function (prepare the sub-class, and deal with problems):
But in this case is the A::foo which call update, not the B::foo. That mean others problems.
3) Any other solution.
As a summary:
How to protect the super-class code against polymorphism?
- Add protections into the super-class.
- Deal with these problem creating the child-class
- The language must do that! (do not know if it is possible with dynamically typed languages)
I am looking for a very theoretical /canonical solution to this question.
EDITED: to take the problem out of the constructor and clarify some points.