typecast-operator

Need clarification on AnyObject in Swift

筅森魡賤 提交于 2019-12-06 06:19:58
Before I start, I have already read the Swift documentation. I am still trying to comprehend what AnyObject actually is. Is it a base class for all objects/classes in Swift, as NSObject is in Objective C? If I create an array of type [AnyObject] and populate it with Movie class instances, that would mean that AnyObject is a base class of the Movie class right? let someObjects: [AnyObject] = [ Movie(name: "2001: A Space Odyssey", director: "Stanley Kubrick"), Movie(name: "Moon", director: "Duncan Jones"), Movie(name: "Alien", director: "Ridley Scott") ] This should be true or else you wouldn't

std::is_convertible for type_info

倾然丶 夕夏残阳落幕 提交于 2019-12-05 23:21:09
问题 In C++11 it is possible to determine if a variable of type A can be implicitly converted to type B by using std::is_convertible<A, B> . This works well if you actually know the types A and B, but all I have is type_infos. So what I'm looking for is a function like this: bool myIsConvertible(const type_info& from, const type_info& to); Is it possible to implement something like that in C++? If so, how? 回答1: It is not possible in portable C++ to do what you want. It may be possible to achieve a

What's the difference between casting using (Object as TClass) and TClass(Object)

倖福魔咒の 提交于 2019-12-05 10:46:58
问题 Got an issue where MyObj.classnameis(TMyClass.classname) is true and TMyClass(MyObj) works but (MyObj as TMyclass).doSomething throws a conversion error. I don't really want any help with that junk, although if you want to put it in the comments that'd be super. I just would like to know what the difference between Obj as Class and Class(Obj) is. 回答1: An as-cast checks the actual object type to make sure the cast is valid, and raises an exception if it's not. A "hard cast" ( TMyClass(MyObj)

In TypeScript, How to cast boolean to number, like 0 or 1

大憨熊 提交于 2019-12-05 01:39:08
As we know, the type cast is called assertion type in TypeScript. And the following code section: // the variable will change to true at onetime let isPlay: boolean = false; let actions: string[] = ['stop', 'play']; let action: string = actions[<number> isPlay]; On compiling, it go wrong Error:(56, 35) TS2352: Neither type 'boolean' nor type 'number' is assignable to the other. Then I try to use the any type: let action: string = actions[<number> <any> isPlay]; Also go wrong. How can I rewrite those code. You can't just cast it, the problem is at runtime not only at compile time. You have a

Why does direct list initialization causes ambiguity for type reference cast if cast operators to the type and reference to the type are declared?

久未见 提交于 2019-12-05 01:20:38
The question rose in context of this answer . Consider an example: struct foo { int value; operator int&(){ return value; } operator int(){ return value; } }; int main () { int &a(foo{}); // #1 //int &b{foo{}}; // #2 -- ambiguity int &c = foo{}; // #3 //int &d = {foo{}}; // #4-- ambiguity int &d { a }; // #5 int &e = { a }; // #6 (void)a; (void)c; (void)d; (void)e; } I don't understand why does #2 and #4 cause ambiguity while #1 and #3 does not. So the question is - why does direct list initialization causes ambiguity for implicit cast to reference if cast operators to the type and reference

Why constructor is not called for given casting operator?

丶灬走出姿态 提交于 2019-12-01 17:23:57
struct A {}; struct B { B (A* pA) {} B& operator = (A* pA) { return *this; } }; template<typename T> struct Wrap { T *x; operator T* () { return x; } }; int main () { Wrap<A> a; B oB = a; // error: conversion from ‘Wrap<A>’ to non-scalar type ‘B’ requested oB = a; // ok } When oB is constructed then Why B::B(A*) is NOT invoked for Wrap<T>::operator T () ? [Note: B::operator = (A*) is invoked for Wrap<T>::operator T () in the next statement] The problem is that the number of user-defined conversions that are invoked implicitly is limited (to 1) by the Standard. B ob = a; implies two user

Why constructor is not called for given casting operator?

折月煮酒 提交于 2019-12-01 17:03:40
问题 struct A {}; struct B { B (A* pA) {} B& operator = (A* pA) { return *this; } }; template<typename T> struct Wrap { T *x; operator T* () { return x; } }; int main () { Wrap<A> a; B oB = a; // error: conversion from ‘Wrap<A>’ to non-scalar type ‘B’ requested oB = a; // ok } When oB is constructed then Why B::B(A*) is NOT invoked for Wrap<T>::operator T () ? [Note: B::operator = (A*) is invoked for Wrap<T>::operator T () in the next statement] 回答1: The problem is that the number of user-defined

Conversion operator in swift

风流意气都作罢 提交于 2019-12-01 09:17:02
Is it possible to write custom conversion (casting) operator in swift ? Especially I'm looking for enums conversion, ex: enum MyEnum : Int { case Case1 = 0 case Case2 func __conversion() -> String { // doesn't work since Swift 1.0 switch self { case Case1: return "Case 1" case Case2: return "Case 2" } } } let enumStr: String = MyEnum.Case1 Of course, I can convert to String with explicit method, but I would like to have implicit mechanism. dfri Disclaimer/TL;DR! This answer pertains to the technical question as to whether we can possibly implement implicit bridging mechanisms between different

What does :: do in PostgreSQL? [duplicate]

北战南征 提交于 2019-11-28 16:48:10
This question already has an answer here: Double colon (::) notation in SQL 4 answers I have seen :: in variety of places involving postgres code I have seen on the net. For example: SELECT '{apple,cherry apple, avocado}'::text[]; It seems to be some sort of cast. What exactly is :: in postgres and when should it be used? I tried a bit of googling and searched the Postgres docs for :: but got no good results. I tried following searches in Google: postgres double colon postgres :: :: I tried the following searches in the postgres docs search button double colon double colon cast :: This was

How to save/load Set of Types?

蓝咒 提交于 2019-11-27 22:07:15
I have this code type TXSample = (xsType1, xsType2, xsType3, xsType4, xsType5, xsType6, xsType6, xsTyp7, xsType8); // up to FXSample30; .. private FXSample = Set of TXSample; .. published property Sample: TXSample read FXSample write FXSample; .. //if Sample has a value of Sample := [xsType2, xsType4, xsType5, xsType6, xsTyp7]; how can i save/load the property of Sample? i would like to save it in the database. is it possible? NGLN Provided your set will never exceed 32 possibilities ( Ord(High(TXSample)) <= 31 ), then it is perfectly fine to typecast the set into an Integer and back: type