unboxing

How to unbox from object to type it contains, not knowing that type at compile time?

余生长醉 提交于 2019-12-13 11:34:15
问题 At the run-time I get boxed instance of some type. How to unbox it to underlying type? Object obj; String variable = "Some text"; obj = variable // boxing; // explicit unboxing, because we know the type of variable at compile time. var x = (String)obj // Now let's pretend that we don't know the type of underlying object at compile time. Type desiredType = obj.GetType(); // But we can figure out. //And now the question. //How to express something like this: var y = (desiredType)obj; //Need to

Why 'if let' does not seem to unbox a value as before in Swift 3 in Xcode 8.3 beta?

人盡茶涼 提交于 2019-12-12 05:59:08
问题 Unlike before, I was surprised to see that 'title' is now an optional (the compiler now generates the waning : String interpolation produces a debug description for an optional value; did you mean to make this explicit?). How it comes the 'if let title =' expression does no unbox it anymore? What should I do to unbox in the if? // Go thru all publication where the tag has been found for item in items { if let item = item as? [String: String?], let title = item["label"] { i += 1 if let

Unboxing to unknown type

不想你离开。 提交于 2019-12-10 13:09:40
问题 I'm trying to figure out syntax that supports unboxing an integral type (short/int/long) to its intrinsic type, when the type itself is unknown. Here is a completely contrived example that demonstrates the concept: // Just a simple container that returns values as objects struct DataStruct { public short ShortVale; public int IntValue; public long LongValue; public object GetBoxedShortValue() { return ShortVale; } public object GetBoxedIntValue() { return IntValue; } public object

Is casting from Number to double allowed in Java 7? (Autoboxing)

我的未来我决定 提交于 2019-12-10 01:31:58
问题 A colleague checked in this code: Number n = ...; double nr = n == null ? 0.0 : (double) n; Another colleague then complained that this didn't compile, and that's what I would expect. However, it turned out that I already had pulled this code from SVN and everything worked fine. We all had our the Java version set to 1.7 in eclipse, and it turned out that the code compiles fine under eclipse 4.4.2 (Luna) but fails under 4.2.2. I fixed the issue by replacing the cast by n.doubleValue() . Now

Boxed Value Type comparisons

吃可爱长大的小学妹 提交于 2019-12-09 14:49:49
问题 What i'm trying to achieve here is a straight value comparison of boxed primitive types. ((object)12).Equals((object)12); // Type match will result in a value comparison, ((object)12).Equals((object)12d); // but a type mismatch will not. (false) object.Equals((object)12,(object)12d); // Same here. (false) I understand the 'why'. I just don't see a 'how'. The types are unknown until runtime, where they could be any primitive type from a datasource. That includes strings, datetimes, bools, etc.

What is the need of an intValue() method if wrappers use unboxing?

一个人想着一个人 提交于 2019-12-08 20:40:32
问题 For example, look at this code: Integer myInt = new Integer(5); int i1 = myInt.intValue(); int i2 = myInt; System.out.println(i1); System.out.println(i2); As you can see, I have two ways of copying my integer value from the wrapper to the primive: I can use unboxing OR I can use the method intValue() So... what's the need of having a method when there is already unboxing? 回答1: Unboxing was introduced in Java 5. The wrappers (including this method) have been there since the original release. A

Why does unboxing enums yield odd results?

不打扰是莪最后的温柔 提交于 2019-12-07 02:42:58
问题 Consider the following:: Object box = 5; int @int = (int)box; // int = 5 int? nullableInt = box as int?; // nullableInt = 5; StringComparison @enum = (StringComparison)box; // enum = OrdinalIgnoreCase StringComparison? nullableEnum = box as StringComparison?; // nullableEnum = null. 2 things:: Why can I unbox to StringComparison ? I guess this is because it's underlying type is Int32 but I still find it odd. Why does nullableEnum have a value of null? As I understand the only valid unboxing

unboxing, (sparse) matrices, and haskell vector library

天涯浪子 提交于 2019-12-05 20:41:04
问题 I would like to manipulate matrices (full or sparse) efficiently with haskell's vector library. Here is a matrix type import qualified Data.Vector.Unboxed as U import qualified Data.Vector as V data Link a = Full (V.Vector (U.Vector a)) | Sparse (V.Vector (U.Vector (Int,a))) type Vector a = U.Vector a As you can see, the matrix is a vector of unboxed vectors. Now, I would like to do a dot product between a vector and a matrix. It is fairly simple to do by combining a sum, zip and map. But if

Why does the Java compiler sometimes allow the unboxing of null?

穿精又带淫゛_ 提交于 2019-12-05 11:14:43
问题 For example: int anInt = null; fails at compile time but public static void main(String[] args) { for (int i = 0; i < 10; i++) { System.out.println("" + getSomeVal()); } } public static int getSomeVal() { return new Random().nextBoolean() ? 1 : null; } fails (usually) at run time. Trying to return just null will also result in a compile error, so I assume there is something about having multiple paths that causes the compiler to infer that null is potentially an autoboxed int ? Why can javac

Boxing and unboxing when using out and ref parameters

风格不统一 提交于 2019-12-05 10:33:10
问题 Does boxing/unboxing occur when a method accepts an out/ref parameter of a ValueType? 回答1: For ref Keyword Its already mentioned on MSDN that : Do not confuse the concept of passing by reference with the concept of reference types. The two concepts are not the same. A method parameter can be modified by ref regardless of whether it is a value type or a reference type. There is no boxing of a value type when it is passed by reference. As for out keyword: The out keyword causes arguments to be