casting

Swift: AnyObject cast to Float failed

混江龙づ霸主 提交于 2019-12-21 22:16:26
问题 let json = [ "left" : 18, "deadline" : "May 10", "progress" : 0.6 ] as [String: AnyObject] let ss = json["progress"] as? Float let sss = json["progress"] as? Double print("ss = \(ss)\n sss = \(sss)") I have no idea why the ss shows nil while sss shows 0.599999998 . Why does casting to Float get nil? Do you guys have some methods so that I can get the correct result? 回答1: The 0.6 is a Double literal value . As such, you can't cast it to Float (you need to convert it). Try this instead: let f =

Casting Proxies - Getting ClassCastException

自作多情 提交于 2019-12-21 21:32:58
问题 I'm getting some weirdness when I'm casting a Dynamic Proxy Class to the object I want it to be. At runtime, under certain conditions, I receive a ClassCastException. In order to explain this better, here are the definitions for the classes/interfaces I want to use. Brackets have been put around any extended interfaces that (should be) irrelevant. public interface CommandSender (extends Permissible) public interface ConsoleCommandSender extends CommandSender, (Conversable) public interface

Swig c++ w/ Java loses type on polymorphic callback functions [duplicate]

Deadly 提交于 2019-12-21 20:44:24
问题 This question already has an answer here : Closed 7 years ago . Possible Duplicate: SWIG Java Retaining Class information of the objects bouncing from C++ Question : Why is my C++ swigged object losing its type when passed to a Java callback function? Setup : I've taken the Swig Java example for doing callbacks and added an object to be passed to the callback run(Parent p) . The callback works as expected but when I pass a Child object the Java seems to lose its type and think its of type

Java get valueOf for generic subclass of java.lang.Number or primitive

萝らか妹 提交于 2019-12-21 19:58:16
问题 After reading through a lot of questions, I asked myself if it is possible to solve the dilemma of transforming a string into a generic number WITHOUT using a hardcoded approach. For example: I get from a method a parameter with the type Class With Number.isAssignableFrom or other ways I can check, if this is a number class. But I also get from the user an input. As a string. The question is: Can I now somehow transform this string into the requested number object without building an if

cast of generic type fails

寵の児 提交于 2019-12-21 19:54:13
问题 I am unable to cast a generic type to another generic type, besides the cast should be valid What I want to archive is in short (for MyModel implementing IModel , and MyImplementation implementing IImplementation ): IImplementation<IModel> implementation = new MyImplementation<MyModel>(); Assert.IsNull(implementation as IImplementation<IModel>); This is a bit confusing, as the type should be valid. Complete conceptual model: interface IModel {} class MyModel : IModel {} interface

How do I cast Class FooObject to class BarObject which both implement interface IObject? [duplicate]

落花浮王杯 提交于 2019-12-21 19:41:13
问题 This question already has answers here : Casting between two types derived from the (same) interface (9 answers) Casting between classes that share the same interface (7 answers) Casting a interface derived class to another derived class (1 answer) Closed 10 months ago . I have 2 classes which implement the same interface. But somehow I cannot cast them from one to another. Here's an example: public interface IObject { // ... } public class FooObject : IObject { // ... } public class

Cast generic class to interface

一个人想着一个人 提交于 2019-12-21 18:06:14
问题 I have a problem with casting a generic class to the interface it is implementing. My code is like this: interface foo { void foobar(); } class bar: foo { public void foobar() { throw new NotImplementedException(); } } now I have my factory that creates instances of my classes by the interface, mostly a simple microkernel (service locator). I will simplify it here. Normally it will look up the implementing class from the configs and the factory take the type as T but that doesn't matter for

Generics explicit conversion

半世苍凉 提交于 2019-12-21 17:41:15
问题 I implemented an explicit conversion from string to object called Foo. So => Foo f = (Foo)"foo data"; works I need to implement a function that cast a string to the generic T, T in this case is Foo datatype. public T Get<T>(object o){ // this always return false if (typeof(T).IsAssignableFrom(typeof(String))) { // when i by pass the if above this throws invalid cast exception return (T)(object)str; } return null; } // When I call this, it generated an error // Invalid cast from 'System.String

Map with multiple value types with advantages of generics

╄→гoц情女王★ 提交于 2019-12-21 17:37:09
问题 I want to create a map that will provide the benefits of generics, whilst supporting multiple different types of values. I consider the following to be the two key advantages of generic collections: compile time warnings on putting wrong things into the collection no need to cast when getting things out of collections So what I want is a map: which supports multiple value objects, checks values put into the map (preferably at compile-time) knows what object values are when getting from the

In R: dcast in function, pass column names (again!)

*爱你&永不变心* 提交于 2019-12-21 17:36:09
问题 Given a df in semi-long format with id variables a and b and measured data in columns m1 and m2 . The type of data is specified by the variable v (values var1 and var2). set.seed(8) df_l <- data.frame( a = rep(sample(LETTERS,5),2), b = rep(sample(letters,5),2), v = c(rep("var1",5),rep("var2",5)), m1 = sample(1:10,10,F), m2 = sample(20:40,10,F)) Looks as: a b v m1 m2 1 W r var1 3 40 2 N l var1 6 32 3 R a var1 9 28 4 F g var1 5 21 5 E u var1 4 38 6 W r var2 1 35 7 N l var2 8 33 8 R a var2 10 29