casting

Smart type casting in python

北战南征 提交于 2020-01-05 14:22:16
问题 I am making api calls and receive a response back in json like so result = {'foo': '123.456', 'bar': '23', 'donald': 'trump', 'time': '2016-04-07T05:31:49.532124Z'} Although result is either a dictionary or a list, the contents are always strings. I want to cast these values to the appropriate type. (i.e. '123.456' to a float, '23' to an int, 'time' to a datetime.) My code works but it seems like there should be a simpler/more efficient way to do this. Is there? Here's my version from

In Scala, fetched value of declared field cast to its class-declared type

偶尔善良 提交于 2020-01-05 11:09:29
问题 I would like to ask how to achieve the following in Scala. Consider scala> case class C(i:Int) defined class C scala> val c = C(1) c: C = C(1) Given a field of interest, in this case scala> val fname = "i" fname: String = i we would like to retrieve the original value and type of field i in c. A first, naive, attempt included the following, scala> val f = c.getClass.getDeclaredField(fname) f: java.lang.reflect.Field = private final int C.i scala> f.setAccessible(true) scala> f.getType res3:

Cast To Interface

吃可爱长大的小学妹 提交于 2020-01-05 08:44:08
问题 I am not getting any errors in my code, but my filter object is always null. When I run the debugger the filter object looks just like the sort object, a list with stuff in it. Although as you can see it is actually a interface.. What do I need to change in this code to access the information in the filter? I guess my main problem is I don't quite fully grasp how to work with interfaces. public IList<Kendo.Mvc.IFilterDescriptor> Filters { get; set; } public IList<Kendo.Mvc.SortDescriptor>

Why do I get a ClassCastException and how do I fix it?

ぐ巨炮叔叔 提交于 2020-01-05 08:09:37
问题 Why do I get a ClassCastException and how do I fix it? I got the following Exception message. Code is below: Exception in thread "main" java.lang.ClassCastException : java.lang.String cannot be cast to java.lang.Integer at HighScores.sort(HighScores.java:32) at HighScores.main(HighScores.java:10) Here is my code: import java.util.*; public class HighScores { public static void main( String args[] ) { ArrayList<String> names = new ArrayList(); ArrayList<Integer> scores = new ArrayList();

How to downcast an instance of a Generic type?

邮差的信 提交于 2020-01-05 07:50:46
问题 I'm struggling with how to properly use C# Generics. Specifically, I want to have a method that takes a Generic Type as a parameter and does different things based on the type of the Generic. But, I cannot "downcast" the Generic Type. See example below. Compiler complains about the cast (Bar<Foo>) saying "Cannot convert type Bar<T> to Bar<Foo> ". But at runtime the cast is OK since I've checked the type. public class Foo { } public class Bar<T> { } // wraps a Bar of generic type Foo public

Cast a raw map to a generic map using a method, cleanly and safely in a fail early manner

被刻印的时光 ゝ 提交于 2020-01-05 07:28:06
问题 Casting, instanceof, and @SuppressWarnings("unchecked") are noisy. It would be nice to stuff them down into a method where they won't need to be looked at. CheckedCast.castToMapOf() is an attempt to do that. castToMapOf() is making some assumptions: (1) The map can't be trusted to be homogeneous (2) Redesigning to avoid need for casting or instanceof is not viable (3) Ensuring type safety in an fail early manner is more important than the performance hit (4) Returning Map<String,String> is

How to invoke a stateless bean method if it is not declared in business interface

谁说胖子不能爱 提交于 2020-01-05 07:13:00
问题 I faced with a problem I can't invoke a method of a bean instance once it isn't declared in the business interface. Basically the problem came to me after I started working with a EJB 2.1 project had been developed by another team. The app server is Websphere v8 The problem is following: We have an abstract class FooAbstract where basic business functions are declared (like read, delete, update, etc.) Each bean must extend that class and implement abstract methods of course. Beans can have

Golang : Type conversion - Converting map[string]string to map[someStruct]string

牧云@^-^@ 提交于 2020-01-05 07:07:07
问题 I have a struct type mapKey string var key1 mapKey = "someKey" var key2 mapKey = "anotherKey" type SampleMap map[mapKey]string Incoming http call has to be map[string]string which I need to typecast to SampleMap in the business logic The normal casting : Sample(request) gives an error, cannot convert type map[string]string to SampleMap. Since these both have the same internal type, why is this error occuring and what is the work around? I really don't want to write a function to map each

Golang : Type conversion - Converting map[string]string to map[someStruct]string

雨燕双飞 提交于 2020-01-05 07:07:05
问题 I have a struct type mapKey string var key1 mapKey = "someKey" var key2 mapKey = "anotherKey" type SampleMap map[mapKey]string Incoming http call has to be map[string]string which I need to typecast to SampleMap in the business logic The normal casting : Sample(request) gives an error, cannot convert type map[string]string to SampleMap. Since these both have the same internal type, why is this error occuring and what is the work around? I really don't want to write a function to map each

Is it safe to cast a pointer to void* then compare to NULL

社会主义新天地 提交于 2020-01-05 04:59:14
问题 i am working on a homework and since our constraints are really strict i need to check for NULL pointers everywhere if i want 100%. So i made a little inlined function which checks pointers for NULL : static inline void exit_on_null(void* ptr, const char* msg) { if ( ! ptr ) { printf("%s\n", msg); exit(1); } } Now i asked myself is it safe to do so? From the standard i know it is save to cast a pointer to void* and back and receive the original pointer. Does that give that the void* version