boxing

Scala strange implicit boxing conversion error

不羁的心 提交于 2019-12-11 02:18:59
问题 Can someone tell me why the following does not work? object TestObject { def map(f: (Double, Double) => Double, x2: Array[Double]) = { val y = x2.zip( x2 ) val z = y.map(f) z } } Produces this error: type mismatch; found : (Double, Double) => Double required: ((Double, Double)) => ? 回答1: In this snippet, f is a function taking two Double parameters and returning a Double . You are attempting to call f by passing a single argument of type Tuple2[Double,Double] . You can fix this by changing

Expand on this autoboxing explanation

谁说胖子不能爱 提交于 2019-12-11 02:18:08
问题 I asked: Is autoboxing/unboxing is done at runtime (JVM) or compile time (compiler)? I received this answer: Autoboxing is achieved by the insertion of method calls and casts by the compiler, into the code. These calls and casts are handled at runtime. Please explain in more detail. 回答1: From Java Specification Chapter 5. Conversions and Promotions Every expression written in the Java programming language has a type that can be deduced from the structure of the expression and the types of the

C++/CLI: Boxing and Generic Lists

牧云@^-^@ 提交于 2019-12-11 01:53:03
问题 I am trying to create a generic list of references to PointF objects. (No, I am not looking to create a generic list of PointF objects.) However, the following line fails to compile: Generic::List<PointF^> ^pointList; // Generates error C3225 On the other hand, creating an array of PointF references works without a problem as follows: array<PointF^> ^points = gcnew array<PointF^>; Here is a sample program: using namespace System; using namespace System::Drawing; namespace Generic = System:

IL constrained call

北城以北 提交于 2019-12-10 16:16:02
问题 For this code: class Program { static void Main() { Console.WriteLine(new MyStruct().ToString()); } struct MyStruct { } } the C# compiler generates constrained callvirt IL code. This article says: For example, if a value type V overrides the Object.ToString() method, a call V.ToString() instruction is emitted; if it does not, a box instruction and a callvirt Object.ToString() instruction are emitted. A versioning problem can arise <...> if an override is later added. So, my question is: why

How do I use the box keyword in pattern matching?

若如初见. 提交于 2019-12-10 15:30:16
问题 This code is shown in The Rust Programming Language: #![feature(box_syntax, box_patterns)] fn main() { let b = Some(box 5); match b { Some(box n) if n < 0 => { println!("Box contains negative number {}", n); } Some(box n) if n >= 0 => { println!("Box contains non-negative number {}", n); } None => { println!("No box"); } _ => unreachable!(), } } But when I run it, the following error occurs: error[E0554]: #[feature] may not be used on the stable release channel I also tried fn main() { let b

Integer vs. int comparison

你离开我真会死。 提交于 2019-12-10 15:19:43
问题 I am new to java . I am now learning the non-primitive Integer type in java . I know the following comparison is not valid and throws a compilation error - String str = "c"; Char chr = 'c'; if(str == chr) return true; The above code snippet gives me the - "Test.java:lineNumber: incomparable types: java.lang.String and char" errors. But I found the following code snippet compiles fine - int a = 1234; Integer aI = 1234; if(a==aI) return true; Here, a is primitive int and aI is non-primitive. So

Curiosity: Converting a C# struct into an object still copies it

。_饼干妹妹 提交于 2019-12-10 11:44:41
问题 This question is more out of curiosity than a real problem. Consider the following code (C# 4.0, if it matters): class Program { static Point myPoint = new Point(3, 5); static void Main(string[] args) { Console.WriteLine("Point Struct Before: " + myPoint); object point = GetPoint(); Console.WriteLine("Point Object Before: " + point); myPoint.X = 10; Console.WriteLine("Point Struct After: " + myPoint); Console.WriteLine("Point Object After: " + point); } static object GetPoint() { return

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

梦想的初衷 提交于 2019-12-10 02:34:51
问题 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"

Why does this cast from short to int fail?

时光总嘲笑我的痴心妄想 提交于 2019-12-10 01:52:48
问题 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

How does the CLR know the type of a boxed object?

风流意气都作罢 提交于 2019-12-10 01:48:11
问题 When a value type is boxed, it is placed inside an untyped reference object. So what causes the invalid cast exception here? long l = 1; object obj = (object)l; double d = (double)obj; 回答1: No, it's not placed in an untyped object. For each value type, there's a boxed reference type in the CLR. So you'd have something like: public class BoxedInt32 // Not the actual name { private readonly int value; public BoxedInt32(int value) { this.value = value; } } That boxed type isn't directly