typechecking

What is the easiest way to do 'is' in Java?

Many languages have a facility to check to see if an Object is of a certain type (including parent subclasses), implemented with 'is' and used like this: if(obj is MyType) Or slightly more tediously you can in other languages check by using the 'as' keyword to do a soft typecast and seeing if the result null. I haven't used Java i...

Enforce strong type checking in C (type strictness for typedefs)

Is there a way to enforce explicit cast for typedefs of the same type? I've to deal with utf8 and sometimes I get confused with the indices for the character count and the byte count. So it be nice to have some typedefs: typedef unsigned int char_idx_t; typedef unsigned int byte_idx_t; With the addition that you need an explicit cast...

What is the best (idiomatic) way to check the type of a Python variable?

I need to know if a variable in Python is a string or a dict. Is there anything wrong with the following code? if type(x) == type(str()): do_something_with_a_string(x) elif type(x) == type(dict()): do_somethting_with_a_dict(x) else: raise ValueError Update: I accepted avisser's answer (though I will change my mind if some...

Force PHP to error on non-declared variables? In objects?

Is there any way to force PHP to blow up (error, whatever) if I misspell a variable name? What about if I'm using an instance of a class and I spell the name of a variable wrong? [I know that I should just get used to it, but maybe there's a way to enforce name checking?] Thanks! Edit: Sorry, that wasn't very specific. Here's the cod...

What is the best way to tell users of my library functions that passed variables are not of the correct type.

I'm currently in the creation of a javascript function library. Mainly for my own use, but you can never be sure if someone else ends up using it in their projects, I'm atleast creating it as if that could happen. Most methods only work if the variables that are passed are of the correct datatype. Now my question is: What is the best way...

Where is the right place parametric type checking in dynamic languages?

Consider the following. (in pseudocode). Class { hello world public constructor(hello, world) { if (hello is not String) { throw Exception() } if (world is not Array) { throw Exception() } this.setHello(hello) this.setWorld(wor...

How do I detect that an object is a generic collection, and what types it contains?

I have a string serialization utility that takes a variable of (almost) any type and converts it into a string. Thus, for example, according to my convention, an integer value of 123 would be serialized as "i:3:123" (i=integer; 3=length of string; 123=value). The utility handles all primitive type, as well as some non-generic collectio...

Checking real variable type in polymorphism (C++)

Suppose we have a class A and class B and C inherit from it. Then we create an array of references to A's, and fill it with B's and C's. Now we decided that we want to eliminate all the C's. Is there a way to check what type each field of the array really holds without doing something redundant like a returnType() function? Edit: fix...

Is there a way to compile-time assert if a variable is a class, struct or a basic type in c++?

I am trying to implement a template class that would be able to tell me if a variable is a class,structure or a basic type. So far I've come with this: template< typename T > class is_class { private: template< typename X > static char ( &i_class( void(X::*)() ) )[1]; // template< typename X > static char ( &i_class...

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...

Comparing expressions of type object

Okay, this is probably very simple but, I have the below "checks" (not at the same time) and the First ALWAYS evaluates to TRUE while the Second SEEMS to work. This actually happens in each place that the row value is a number or bool(Date seems fine...). If I walk through the code in Debug it shows the value of row["PersonID"] as 16...

OCaml: Why does this code produce a type check error?

Here is my code: let avg l = List.fold_left ( +. ) 0. l /. float (List.length l);; let variability l = let xbar = avg l in let odp = (List.map (fun i -> ((float) i -. xbar) ** 2.0) l) in let sum = List.fold_left ( +. ) 0. odp in sum /. (float) length l;; Entering this into the toplevel produces the following: val...

Is it possible to avoid using type checking in this example?

Sorry for the poor title, can't think of a succinct way of putting this.. I'm thinking of having a list of objects that will all be of a specific interface. Each of these objects may then implement further interfaces, but there is no guarantee which object will implement which. However, in a single loop, I wish to be able to call the me...

Subclass check, is operator or enum check

Hi A couple of friends was discussing the use of inheritance and how to check if a subclass is of a specific type and we decided to post it here on Stack. The debate was about if you should implement a abstract enum in the base class to be used for checking the type of the subclass, or if you should use the is operator. Alt 1. public ...

Java snippet that causes stack overflow in the compiler or typechecker (javac)?

Yesterday at a seminar the presenter showed a small java program, with 3 classes, featuring both co-variance and contra-variance. When attempting to compile using javac, the type checker will throw a StackOverflowException. The snippet is developed by some guys that work at Microsoft (think one was called Kennedy). Can't find it using...

Is there a way to have SQL server validate object references in Stored Procs?

The following code executes fine in SQL Server create proc IamBrokenAndDontKnowIt as select * from tablewhichdoesnotexist Of course if I try to run it, it fails with Invalid object name 'tablewhichdoesnotexist'. Is there any way to compile or verify that Stored Proc are valid? ...

Somehow not assigning a class with Ruby

On runtime, my code often come into an undefined method error for the method mate. As far as I can figure, a Person somehow slips through the cracks sometime along the code's exucution, and manages not to have an allele assigned to it. Code (disclaimer, not the best formatted): class Allele attr_accessor :c1, :c2 def initialize(c1...

C++ type-checking at compile-time

Hi, all. I'm pretty new to C++, and I'm writing a small library (mostly for my own projects) in C++. In the process of designing a type hierarchy, I've run into the problem of defining the assignment operator. I've taken the basic approach that was eventually reached in this article, which is that for every class MyClass in a hierarchy ...

Check if something is a list

What is the easiest way to check if something is a list? A method doSomething has the parameters a and b. In the method, it will loop through the list a and do something. I'd like a way to make sure a is a list, before looping through - thus avoiding an error or the unfortunate circumstance of passing in a string then getting back a let...

Which is the most accurate way to check variables type JavaScript?

I need to check the type of a variable in JavaScript. I know 3 ways to do it: instanceof operator: if(a instanceof Function) typeof operator: if(typeof a=="function" toString method (jQuery uses this): Object.prototype.toString.call(a) == "[object Function]" Which is the most accurate way to do type checking beetween these solutions?...