type-checking

Should I check the types of constructor arguments (and at other places too)?

Python discourages checking the types. But in many cases this may be useful: Checking constructor arguments. e.g. checking foe Boolean, string, dict etc. If I don't and set the object's members to the arguments it will cause problems later. Checking functions arguments. In properties. If someone sets a wrong value or different type, I ...

Type checking of arguments Python

Duplicate of What is the best (idiomatic) way to check the type of a Python variable? Sometimes checking of arguments in Python is necessary. e.g. I have a function which accepts either the address of other node in the network as the raw string address or class Node which encapsulates the other node's information. I use type(0 functi...

Can someone explain these few lines of MSIL? Why does it move a value off the evaluation stack to a local variable, only to move it back immediately and return it?

The following MSIL code loads a single argument (a string), calls a method, which returns bool, and then returns that bool value. What I don't understand is why it calls stloc.0 to store the method's return value in a local variable, then performs an explicit unconditional control transfer to the very next labeled line (seems unnecessar...

What are the limits of type checking and type systems?

Type systems are often criticised, for being to restrictive, that is limiting programming languages and prohibiting programmers to write interesting programmes. Chris Smith claims: We get assurance that the program is correct (in the properties checked by this type checker), but in turn we must reject some interesting programs. a...

OCaml: Type Checking Objects

If I have an object, how can I determine its type? (Is there an OCaml equivalent to Java's instanceof operator?) ...

Java: Instanceof and Generics

Before I look through my generic data structure for a value's index, I'd like to see if it is even an instance of the type this has been parametrized to. But Eclipse complains when I do this: @Override public int indexOf(Object arg0) { if (!(arg0 instanceof E)) { return -1; } What is the better way to do it? This is the error mes...

is there a way to check if a param contains a class or a class instance?

I want the wrapper my_function to be able to receive either a class or class instance, instead of writing two different functions: >>> from module import MyClass >>> my_function(MyClass) True >>> cls_inst = MyClass() >>> my_function(cls_inst) True the problem is that I don't know in advance which type of classes or class instance...

Is it Pythonic to check function argument types?

I know, type checking function arguments is generally frowned upon in Python, but I think I've come up with a situation where it makes sense to do so. In my project I have an Abstract Base Class Coord, with a subclass Vector, which has more features like rotation, changing magnitude, etc. Lists and tuples of numbers will also return Tr...

Checking type parameter of a generic method in C#

Is it possible to do something like this in C#: public void DoSomething<T>(T t) { if (T is MyClass) { MyClass mc = (MyClass)t ... } else if (T is List<MyClass>) { List<MyClass> lmc = (List<MyClass>)t ... } } ...

Is there a way to enforce runtime type-checking in Objective-C on Cocoa?

Hi I'm finding a way to enforce runtime type checking or such things in Objective-C on Cocoa. This is my code sample. I expected runtime error about wrong assignment to variable 'b'. But it wasn't. Compiled and executed without any error. #import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePoo...

Is checking Perl function arguments worth it?

There's a lot of buzz about MooseX::Method::Signatures and even before that, modules such as Params::Validate that are designed to type check every argument to methods or functions. I'm considering using the former for all my future Perl code, both personal and at my place of work. But I'm not sure if it's worth the effort. I'm thinking...

How Can I Check an Object to See its Type and Return A Casted Object

I have method to which I pass an object. In this method I check it's type and depending on the type I do something with it and return a Long. I have tried every which way I can think of to do this and I always get several compiler errors telling me it expects a certain object but gets another. Can someone please explain to me what I a...

How to check the type of a Parameter in an Expression Tree (similar to 'is' keyword)

How do I check the type of a Parameter in an Expression Tree (and get the Expression Tree equivalent of a bool if it the right type)? If it were normal code, I would do this: if(myObj is int) I see there is a Expression.Convert method but this converts the object instead of just checking its type. EDIT: I found the answer, you use a ...

Writing an extension method for type T; how can I add a type constraint for a field of T?

Initial situation: I am working with a proprietary framework (ESRI's ArcGIS Engine) which I want to extend with some new functionality. I've chosen to use extension methods in C# for this. Shown below are the parts of the framework API that are relevant to this question: +------------------------+ IGeometry |...

Is a type-safe respond_to in scala possible?

Using a case construct for type-safe casting is easily done in scala. The following code ensures that square gets only called on objects which have an according type. class O class A extends O { def square(i: Int):Int = { i*i } } class B extends O { def square(d: Double):Double = { d*d } } class C extends O {} def square(o: O) ...

Strong typedef static checker (unix)

Is there a free tool (some kind of a static checker) that does typedef-based type-checking for a plain C (not C++) and runs on Linux (or any kind of free Unix)? I am aware of a commercial one: PC-lint/FlexeLint. It does exactly what I want, but it's not free and Windows-only. Here is an example from it's manual: typedef int Count; typed...