casting

Spring getBean with type validation

我只是一个虾纸丫 提交于 2019-12-18 21:12:52
问题 I'm using the method ApplicationContext.getBean(String name, Class requiredType). The bean is of type util:set. My code looks like: Set<String> mySet = context.getBean("myBean", Set.class); I want to know is how to do something like this to avoid the type casting warning: Set<String> mySet = context.getBean("myBean", Set<String>.class); I'm not sure if it's possible to define the type of a class in this way. Am I dreaming or is there a way to do this? Thanks. 回答1: Not really but there is a

How does conversion operator return a value?

我的未来我决定 提交于 2019-12-18 18:51:24
问题 For a class A, an integer conversion operator would look something like; operator int() //Here we don't specify any return type { return intValue; } How is the above function able to return a value when its return value type appears not to be specified? It doesn't appear to return "anything", but I know it's not void . How is this meaningful when a return type is not specified? 回答1: The return type of operator T() is always T . It's a special case of C++. It does not use standard function

Angular cast select value to int

你。 提交于 2019-12-18 18:35:36
问题 I have a form with different selects like : <select [(ngModel)]="selected.isConnected" (ngModelChange)="formChanged()" name="etat" id="etat" class="form-control"> <option value="0">Not connected</option> <option value="1">Connected</option> </select> My backend expect to receive an int in the "isConnected" attribute. Unfortunately as soon as I change the value of the select the attribute is cast to a string : { isConnected : "0", // 0 expected } For standard <input> I could use type="number"

How to determine the represented type of enum value?

為{幸葍}努か 提交于 2019-12-18 17:31:13
问题 Consider the following two enums: enum MyEnum1 { Value1 = 1, Value2 = 2, Value3 = 3 } enum MyEnum2 { Value1 = 'a', Value2 = 'b', Value3 = 'c' } I can retrieve the physical value represented by these enum values through explicit casting, ((int)MyEnum1.Value2) == 2 or ((char)MyEnum2.Value2) == 'b' , but what if I want to get the char representation or the int representation without first knowing the type to cast to? Is it possible to get the underlying value of an enum without a cast or is it

return single instance object as IEnumerable

时光怂恿深爱的人放手 提交于 2019-12-18 14:48:28
问题 I have in instance of class foo and i want to return it as IEnumerable. Can i do it without creating a new list etc.. Perhaps something like the following: IEnumerable<foo>.fromInstance(foo) 回答1: Options: Create an instance of a collection class, like an array or a list. This would be mutable by default, which would be slightly unhelpful if this is a sequence you want to be able to hand out in your API. You could create a ReadOnlyCollection<T> wrapper around such a collection though. Write

Shouldn't this code throw an ambiguous conversion error?

五迷三道 提交于 2019-12-18 14:45:16
问题 I have two classes, A and B , each defining a conversion to B . A has a conversion operator to B , B has a constructor from A . Shouldn't a call to static_cast<B> be ambiguous? Using g++ this code compiles and chooses the conversion constructor. #include<iostream> using namespace std; struct B; struct A { A(const int& n) : x(n) {} operator B() const; //this const doesn't change the output of this code int x; }; struct B{ B(const double& n) : x(n) {} B(const A& a); double x; }; A::operator B()

C# generic implicit cast on Interface failed

二次信任 提交于 2019-12-18 14:14:22
问题 Why will the below not compile? What's special about the interface that causes the compiler to think it can't cast from Container<T> to T , when T is an interface? I don't think its a covariant issue, as I'm not downcasting, but perhaps it is. This is quite like Why C# compiler doesn't call implicit cast operator? but I don't think it's quite the same. Product pIn =null; Product pOut; Container<Product> pContainer; List<Product> pListIn = null; List<Product> pListOut; Container<List<Product>>

PHP: casting to a dynamically determined type

ぐ巨炮叔叔 提交于 2019-12-18 13:52:43
问题 I'd like do something like this: $type = "int"; $casted = ($type)$value; This doesn't work. But is there some other way to cast to a dynamically determined type? Thanks 回答1: Use the settype() function. http://php.net/manual/en/function.settype.php Example: $type = 'int'; $var = '20'; settype($var, $type); var_dump($var); 来源: https://stackoverflow.com/questions/4687967/php-casting-to-a-dynamically-determined-type

How to detect that parameter is a tuple of two arbitrary types?

本秂侑毒 提交于 2019-12-18 13:36:30
问题 What I am actually doing is more complex but it comes down to being able to implement function to detect that something is a tuple, regardless of what the are the types of its elements. This is my approch that does not work (see comment on last line) : func isTuple(b: Any) -> Bool { return b is (Any, Any) } let myString = "aa" let myDouble = 1.2 isTuple((myString, myDouble)) //returns false Why doesn't it work? Shouln't Any act as a "wildcard" in tuples as well? Is it a known Swift bug (if

how to cast an array of char into a single integer number?

你。 提交于 2019-12-18 13:28:20
问题 i'm trying to read contents of PNG file. As you may know, all data is written in a 4-byte manner in png files, both text and numbers. so if we have number 35234 it is save in this way: [1000][1001][1010][0010]. but sometimes numbers are shorter, so the first bytes are zero, and when I read the array and cast it from char* to integer I get wrong number. for example [0000] [0000] [0001] [1011] sometimes numbers are misinterpreted as negative numbers and simetimes as zero! let me give you an