boxing

How can I reuse a box that I have moved the value out of?

左心房为你撑大大i 提交于 2019-12-05 02:03:16
I have some non-copyable type and a function that consumes and (maybe) produces it: type Foo = Vec<u8>; fn quux(_: Foo) -> Option<Foo> { Some(Vec::new()) } Now consider a type that is somehow conceptually very similar to Box : struct NotBox<T> { contents: T } We can write a function that temporarily moves out contents of the NotBox and puts something back in before returning it: fn bar(mut notbox: NotBox<Foo>) -> Option<NotBox<Foo>> { let foo = notbox.contents; // now `notbox` is "empty" match quux(foo) { Some(new_foo) => { notbox.contents = new_foo; // we put something back in Some(notbox) }

Converting from Java primitive to wrapper classes

流过昼夜 提交于 2019-12-05 00:15:18
问题 I am mystified by the behavior of the Java compiler when assigning primitives to wrapper class references. Please see the code below. The lines with comments don't compile. I don't understand the logic of why: a byte can be assigned to a Byte or Short , but not Integer or Long reference a short can be assigned to a Byte or Short , but not Integer or Long reference an int can be assigned to a Byte , Short , or Integer , but not Long reference a long can be assigned to a Long , but not Byte ,

Why does this cast from short to int fail?

给你一囗甜甜゛ 提交于 2019-12-04 23:53:51
We have some code that archives data from a Microsoft Access database into a MS SQL Server database. Assuming we have a data reader already populated from the Access table and we are adding a parameter to a SqlCommand in preparation for the insert, we have a typecast that is failing. Here is the code: oSqlServerDbCmd_ForInsert.Parameters.AddWithValue("@Duration", (int) oReader["Duration"]); The field from the oReader is actually an Access Integer, which is a short in C#. If we cast to a short here there is no problem. However, if we cast to an int the code throws an InvalidCastException. I may

Cast Boxed Object back to Original Type

限于喜欢 提交于 2019-12-04 17:46:00
问题 I expect there's one of two answers to this, either impossible or extremely simple and I've overlooked the obvious Google query. The underlying issue is that I have a generic object being passed in via an EventHandler that boxes the object and obfuscates the true type; only at runtime do I know what the object is. Admittedly the dynamic keyword can get around the issue, but I'd like to not lose IntelliSense and everything if I can avoid it. Plus, it doesn't solve not knowing what each of the

How to unbox elements contained in polymorphic vectors?

瘦欲@ 提交于 2019-12-04 13:25:06
After reading this answer to "Vector of objects belonging to a trait" , it looks like Rust does automatic unboxing. Is this the case? My code doesn't compile and I don't understand how that answer's code could compile. What is the correct way to unbox the elements of a polymorphic vector, one containing boxed traits? I've read Rust by Example and the Box documentation and I can't see any method that looks like unbox() . My code is: trait HasArea { fn area(&self) -> f64; } struct Circle { x: f64, y: f64, radius: f64, } impl HasArea for Circle { fn area(&self) -> f64 { std::f64::consts::PI *

Enum Boxing and Equality

怎甘沉沦 提交于 2019-12-04 10:17:57
问题 Why does this return False public enum Directions { Up, Down, Left, Right } static void Main(string[] args) { bool matches = IsOneOf(Directions.Right, Directions.Left, Directions.Right); Console.WriteLine(matches); Console.Read(); } public static bool IsOneOf(Enum self, params Enum[] values) { foreach (var value in values) if (self == value) return true; return false; } while this returns True? public static bool IsOneOf(Enum self, params Enum[] values) { foreach (var value in values) if

How is Nullable<T> different from a similar custom C# struct?

不打扰是莪最后的温柔 提交于 2019-12-04 09:54:07
In Nullable micro-optimizations, part one , Eric mentions that Nullable<T> has a strange boxing behaviour that could not be achieved by a similar user-defined type. What are the special features that the C# language grants to the predefined Nullable<T> type? Especially the ones that could not be made to work on a MyNullable type. Of course, Nullable<T> has special syntactic sugar T? , but my question is more about semantics. Eric Lippert What I was getting at is: there is no such thing as a boxed nullable . When you box an int , you get a reference to a boxed int . When you box an int? , you

Java interoperability woes with Scala generics and boxing

大兔子大兔子 提交于 2019-12-04 09:30:59
问题 Suppose I've got this Scala trait: trait UnitThingy { def x(): Unit } Providing a Java implementation is easy enough: import scala.runtime.BoxedUnit; public class JUnitThingy implements UnitThingy { public void x() { return; } } Now let's start with a generic trait: trait Foo[A] { def x(): A } trait Bar extends Foo[Unit] The approach above won't work, since the unit x returns is now boxed, but the workaround is easy enough: import scala.runtime.BoxedUnit; public class JBar implements Bar {

Boxed nullable underlying type can be cast to enum but boxed enum type can't be cast to nullable type

我们两清 提交于 2019-12-04 06:58:12
Boxed nullable underlying type can be cast to enum but boxed enum type can't be cast to nullable type. And similarly, Boxed nullable enum can be cast to underlying type but boxed underlying type can't be cast to nullable enum. Ok, I know "boxed nullable type" is not the best way to describe it, but it's for the sake of the question. I'm aware it's the underlying value type that's getting boxed. I will show it with examples. Assume I have an enum with int as the underlying type. enum Sex { Male, Female } Case I: int? i = 1; object o = i; Sex e = (Sex)o; //success //but Sex e = Sex.Male; object

Is boxing involved when calling ToString for integer types?

元气小坏坏 提交于 2019-12-04 02:54:14
Very simple question: int a = 5; string str = a.ToString(); Since ToString is a virtual method of System.Object, does it mean that everytime I call this method for integer types, a boxing occurs? You've already got answers telling you that when ToString() is overridden for a value type, there will be no boxing when you call it, but it's nice to have some way of actually seeing that. Take the type int? ( Nullable<int> ). This is a useful type because it is a value type, yet boxing may produce a null reference, and instance methods cannot be called through a null reference. It does have an