static-typing

Static type of the exception object

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-14 01:38:37
问题 I read the following from C++ Primer (5th edition, Section 18.1.1): "When we throw an expression, the static, compile-time type of that expression determines the type of the exception object." So I tried the following code: #include <iostream> class Base{ public: virtual void print(std::ostream& os){os << "Base\n";} }; class Derived: public Base{ public: void print(std::ostream& os){os << "Derived\n";} }; int main(){ try{ Derived d; Base &b = d; b.print(std::cout); //line 1 throw b; } catch

C++ is static typed language, why can we get type at runtime

南笙酒味 提交于 2019-12-11 08:57:46
问题 TYPE& dynamic_cast<TYPE&> (object); TYPE* dynamic_cast<TYPE*> (object); For example we can get type like this. C++ is static typed language, why can we get type at runtime 回答1: Variables in C++ have a statically determined type. Objects don't necessarily. You only need to handle objects through variables of statically known type. Examples: int * p = new int[argc]; // there is an object whose dynamic type is int[argc] Base * p = new Derived; // *p has dynamic type Derived Base * q = rand() % 2

How to make mypy complain about assigning an Any to an int (part 2)

不羁的心 提交于 2019-12-11 08:13:32
问题 (This is a follow-up to this question.) My code base is fully statically typed (annotation) but at some points there is the Any type, for example because a value was parsed from a JSON string. Here is my minimal example: import json from typing import Any, Dict, Union def main() -> None: data = json.loads('{"value" = "three"}') my_int: int = data['value'] if __name__ == "__main__": main() mypy --strict accepts this code. However I would like to find these places automatically, to take the

Is “Lisp-1 vs Lisp-2” relevant in a language with static types?

断了今生、忘了曾经 提交于 2019-12-11 06:59:52
问题 (This is a CS-theory type of question; I hope that's acceptable.) The "Lisp-1 vs Lisp-2" debate is about whether the namespace of functions should be distinct from the namespace of all other variables, and it's relevant in dynamically typed languages that allow the programmer to pass around functions as values. Lisp-1 languages (such as Scheme) have one namespace, so you can't have both a function named f and also an integer named f (one would shadow the other, just like two integers named f

How to make mypy complain about assigning an Any to an int

独自空忆成欢 提交于 2019-12-11 06:36:51
问题 mypy --strict dutifully complains about the following code: from typing import Any, Dict def main() -> None: my_str: str = 'hello' my_int: int = my_str if __name__ == "__main__": main() by outputting: error: Incompatible types in assignment (expression has type "str", variable has type "int") However the following code is accepted without any error: from typing import Any, Dict def main() -> None: my_str: Any = 'hello' my_int: int = my_str if __name__ == "__main__": main() Is there an option

Common Lisp type declarations not working as expected

北战南征 提交于 2019-12-10 13:56:20
问题 When I define a function in Common Lisp like this: (defun foo (n) (declare (type fixnum n)) (+ n 42)) I expected a call like (foo "a") to fail right away but it instead fail at the call to + . Is the declare form not guarantees static type checking? 回答1: Type declarations are traditionally meant to be used as guarantees to the compiler for optimization purposes. For type checking, use check-type (but note that it, too, does the checking at run-time, not at compile-time): (defun foo (n) (check

Simulating aspects of static-typing in a duck-typed language

丶灬走出姿态 提交于 2019-12-10 12:49:41
问题 In my current job I'm building a suite of Perl scripts that depend heavily on objects. (using Perl's bless() on a Hash to get as close to OO as possible) Now, for lack of a better way of putting this, most programmers at my company aren't very smart. Worse, they don't like reading documentation and seem to have a problem understanding other people's code. Cowboy coding is the game here. Whenever they encounter a problem and try to fix it, they come up with a horrendous solution that actually

Tired of non-semantic testing to make up for dynamic typing - suggestions?

一笑奈何 提交于 2019-12-10 12:44:02
问题 I used to do a lot of web programming in Rails (PHP before that) before I started studying computer engineering. Since then, I've done a lot of school work in C, and some personal stuff in Objective-C (Mac stuff). I learnt to love static typing. But now I'm having to do some professional web development (freelancing) and have picked up Rails once again. I'm finding it really annoying to write non-semantic type-checking tests. I was getting those for free from C and Objective-C compilers. I

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

Enforcing types on untyped data in TypeScript

别说谁变了你拦得住时间么 提交于 2019-12-06 05:54:41
问题 I'm wondering if there are any tools or techniques that are being used to do low level validation on object data when using TypeScript. An example would be a JSON body of a POST request on a HTTP service. Typically I've created an interface for the expected data and then cast the data to that interface but I'm aware that this is superficial. Example: router.route('/supercres') .get((req, res, next) => { const typedBody = <SuperCresBody>req.body; }) interface SuperCresBody { name: string, yoyo