extracting data from typing types

Checking if a variable conforms to a typing object To check if string_list conforms to string_list_class, you can use the typeguard type checking library. from typeguard import check_type try: check_type(‘string_list’, string_list, string_list_class) print(“string_list conforms to string_list_class”) except TypeError: print(“string_list does not conform to string_list_class”) Checking the generic type of a typing object To check if … Read more

Is This Use of the “instanceof” Operator Considered Bad Design?

The Visitor pattern is typically used in such cases. Although the code is a bit more complicated, but after adding a new RecordType subclass you have to implement the logic everywhere, as it won’t compile otherwise. With instanceof all over the place it is very easy to miss one or two places. Example: public abstract … Read more

Java instanceof with changing objects

How about this: public boolean checker(Object obj) { return obj instanceof SomeClass; } or if SomeClass needs to be a parameter: public boolean checker(Object obj, Class someClass) { return someClass.isInstance(obj); } or if you want the instance to be someClass and NOT an instance of a subclass of someClass: public boolean checker(Object obj, Class someClass) … Read more

Why cast after an instanceOf?

Old code will not work correctly The implied cast feature is justified after all but we have trouble to implement this FR to java because of backward-compatibility. See this: public class A { public static void draw(Square s){…} // with implied cast public static void draw(Object o){…} // without implied cast public static void main(String[] … Read more

JavaScript inheritance and the constructor property

Okay, let’s play a little mind game: From the above image we can see: When we create a function like function Foo() {}, JavaScript creates a Function instance. Every Function instance (the constructor function) has a property prototype which is a pointer. The prototype property of the constructor function points to its prototype object. The … Read more

instanceof check in EL expression language

You could compare Class#getName() or, maybe better, Class#getSimpleName() to a String. <h:link rendered=”#{model[‘class’].simpleName eq ‘ClassA’}”> #{errorMessage1} </h:link> <h:link rendered=”#{model[‘class’].simpleName eq ‘ClassB’}”> #{errorMessage2} </h:link> Note the importance of specifying Object#getClass() with brace notation [‘class’] because class is a reserved Java literal which would otherwise throw an EL exception in EL 2.2+. The type safe alternative is … Read more

Java: instanceof Generic

Generics are a compile time feature. Generics add checks at compile time which may not have any meaning at runtime. This is one example. You can only check the type of the object referenced which could be a super type in code. If you want to pass the type T you have do this explicitly. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)