‘casting’ with reflection
void SetValue(PropertyInfo info, object instance, object value) { info.SetValue(instance, Convert.ChangeType(value, info.PropertyType)); }
void SetValue(PropertyInfo info, object instance, object value) { info.SetValue(instance, Convert.ChangeType(value, info.PropertyType)); }
is there a possibility that casting a double created via Math.round() will still result in a truncated down number No, round() will always round your double to the correct value, and then, it will be cast to an long which will truncate any decimal places. But after rounding, there will not be any fractional parts … Read more
As of CakePHP 3.2 you can use Query::selectTypeMap() to add further types, which are only going to be used for casting the selected fields when data is being retrieved. $query = $table ->find() ->select([‘alias’ => ‘actual_field’, /* … */]); $query ->selectTypeMap() ->addDefaults([ ‘alias’ => ‘integer’ ]); You can use any of the built-in data types, … Read more
The program has undefined bahaviour because you may not change a const object. From the C++ Standard 4 Certain other operations are described in this International Standard as undefined (for example, the effect of attempting to modify a const object). [ Note: This International Standard imposes no requirements on the behavior of programs that contain … Read more
The problem is that you don’t cast an object to an int, you’re attempting to unbox an int. The object really has to be an int. It cannot be just anything that can be converted to an int. So the difference is that this: int a = (int)obj; Really needs obj to be a boxed … Read more
Java generics use type erasure, meaning those parameterized types aren’t retained at runtime so this is perfectly legal: List<String> list = new ArrayList<String>(); list.put(“abcd”); List<Integer> list2 = (List<Integer>)list; list2.add(3); because the compiled bytecode looks more like this: List list = new ArrayList(); list.put(“abcd”); List list2 = list; list2.add(3); // auto-boxed to new Integer(3) Java generics … Read more
Such a conversion would allow you to put a const char* into your array of char*, which would be unsafe. In print you could do: thing[0] = “abc”; Now argv[0] would point to a string literal that cannot be modified, while main expects it to be non-const (char*). So for type safety this conversion is … Read more
This is because numbers that have a finite representation in base 10 may or may not have an exact representation in the floating point representation PHP uses. See >php -r “echo var_dump(sprintf(‘%.40F’, 39.3 * 100.0));” string(45) “3929.9999999999995452526491135358810424804688” Since int always rounds the number down, a small error in the representation makes the cast round it … Read more
So why my explicit cast work, and the one inside .Cast<> doesn’t? Your explicit cast knows at compile time what the source and destination types are. The compiler can spot the explicit conversion, and emit code to invoke it. That isn’t the case with generic types. Note that this isn’t specific to Cast or LINQ … Read more
Are C-structs with the same members types guaranteed to have the same layout in memory? Almost yes. Close enough for me. From n1516, Section 6.5.2.3, paragraph 6: … if a union contains several structures that share a common initial sequence …, and if the union object currently contains one of these structures, it is permitted … Read more