casting

Why PHP casts two numerical strings to numbers before [loosely] comparing them?

点点圈 提交于 2019-12-19 13:04:05
问题 I browsed through several similar questions, but they all only state the fact: If ... comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. Okay, I got it. It explains what is going on when '00001' == '1' returns TRUE . The question is: Why PHP does so? What is the reason for probing strings for being numeric, and then casting if so? Why can't we just compare two strings already? I can fairly understand what casting is

Easy assignments with empty square brackets? x[]<-

隐身守侯 提交于 2019-12-19 12:51:15
问题 While looking at an answer posted recently on SO, I noticed an unfamiliar assignment statement. Instead of the usual form of myVar<- myValue , it used the for myVar[]<- myValue . Personally, I had never seen such an assignment, but it had a highly useful effect-- it reshaped the assigned data myValue to the shape of myVar. I would like to use this in my code. However the documentation for "<-" seems to be silent on it. Is this a well established feature and one can rely on it to work in all

Generic cast method returning base type (struct)

此生再无相见时 提交于 2019-12-19 12:08:07
问题 I'd like to write a method that cast a value (object) into a basic type (like string, int, double, etc..). I use this method in a routine that maps DataRows on objects. I wrote this: public static T CastObjectToBasicType<T>(DataRow row, string column) where T : struct { object cellValue = row[column]; if (typeof(T) == typeof(string)) return ToString(cellValue); if (typeof(T) == typeof(int) || typeof(T) == typeof(int?)) return ToInt(cellValue); if (typeof(T) == typeof(bool) || typeof(T) ==

Returning the subclass in a UIViewController static

放肆的年华 提交于 2019-12-19 11:43:57
问题 Consider a base UIViewController class... class Rooms: UIViewController { class func instantiate()->Rooms { } static func make()->Rooms { let emplacedAndSetup = self.instantiate() // various kodes here // very likely put s.view somewhere return emplacedAndSetup } sundryOtherFunctionality() } (Note the self. before instantiate() which seems to be necessary to get "that" instantiator.) Each subclass knows its own storyboard ID to how use to instantiateViewController : class Dining: Rooms {

what is the use of “static_cast<void>” in macro?

偶尔善良 提交于 2019-12-19 10:58:10
问题 I'm seeing a macro definition like this: #define ASSERT_VALID_PARAM(param, assertion) { static_cast<void>(param); if (!(assertion)) { throw InvalidParamError(#param, #assertion, __FILE__, __PRETTY_FUNCTION__, __LINE__); } } I'm not able to figure out the need of static_cast<void>(param) here. Any idea on why this is needed? 回答1: This macro is designed to validate a certain real parameter passes a certain validation rule(s). The logic part of the macro is composed of 2 parts: Validate that

Cannot cast between generic types

雨燕双飞 提交于 2019-12-19 10:15:24
问题 I'm trying to do this: interface IA { } class A : IA { } class Foo<T> where T: IA { } class Program { static void Main( string[] args ) { Foo<A> fooA = new Foo<A>(); Foo<IA> fooIA = fooA as Foo<IA>; } } However, the cast from Foo<A> to Foo<IA> does not compile. I recall seeing covariance issues like this when casting between List<T> 's, but I didn't think it applied to simple generics like this. What is a good work around to getting this cast to work? How do I solve this problem? 回答1: All

How to check if a void* pointer can be safely cast to something else?

若如初见. 提交于 2019-12-19 09:09:49
问题 Let's say I have this function, which is part of some gui toolkit: typedef struct _My_Struct My_Struct; /* struct ... */ void paint_handler( void* data ) { if ( IS_MY_STRUCT(data) ) /* <-- can I do something like this? */ { My_Struct* str = (My_Struct*) data; } } /* in main() */ My_Struct s; signal_connect( SIGNAL_PAINT, &paint_handler, (void*) &s ); /* sent s as a void* */ Since the paint_handler will also be called by the GUI toolkit's main loop with other arguments, I cannot always be sure

How does method reference casting work?

 ̄綄美尐妖づ 提交于 2019-12-19 08:59:40
问题 public class Main { interface Capitalizer { public String capitalize(String name); } public String toUpperCase() { return "ALLCAPS"; } public static void main(String[] args) { Capitalizer c = String::toUpperCase; //This works c = Main::toUpperCase; //Compile error } } Both are instance methods with same signature. Why does one work and the other doesn't? Signature of String::toUpperCase : String toUpperCase(); 回答1: There are 3 constructs to reference a method: object::instanceMethod Class:

R floating point number precision being lost on coversion from character

天涯浪子 提交于 2019-12-19 08:54:13
问题 I have a large floating point number as a character like so x<-"5374761693.91823"; On doing as.numeric(x); I get the following output 5374761694 I would like to preserve the floating point nature of the number while casting. 回答1: use digits argument in print to see the actual number: > print(as.numeric(x), digits=15) [1] 5374761693.91823 options is another alternative: > options(digits=16) > as.numeric(x) [1] 5374761693.91823 > # assignments > options(digits=16) > y <- as.numeric(x) > y [1]

What is the (type) in (type)objectname.var

青春壹個敷衍的年華 提交于 2019-12-19 08:31:43
问题 I am going through a book on C# and have come across something that I can't seem to look up, because I don't know what it is called, or trying to search for something by description. Could some explain to me what is going on, or the meaning behind the (type) that comes before a reference to an object as in (int)objectname.variablename ? It seems like casting to me. EDIT: Since most of you are going off 'My' reference to casting when I am only guessing, and needed more code, I am including the