casting

What is difference between “as” and “is” operator in Kotlin?

余生长醉 提交于 2020-08-19 03:15:12
问题 In Java, I can write code like: void cast(A a) { if(a instanceof Person) { Person p = (Person) a; } } In Kotlin, what should I do? Use as operator or is operator? 回答1: is X is the equivalent of instanceof X foo as X is the equivalent of ((X) foo) Additionally, Kotlin performs smart casting where possible, so no additional cast needed after you check the type using is : open class Person : A() { val foo: Int = 42 } open class A and then: if (p is Person) { println(p.foo) // look, no cast

What is difference between “as” and “is” operator in Kotlin?

此生再无相见时 提交于 2020-08-19 03:12:04
问题 In Java, I can write code like: void cast(A a) { if(a instanceof Person) { Person p = (Person) a; } } In Kotlin, what should I do? Use as operator or is operator? 回答1: is X is the equivalent of instanceof X foo as X is the equivalent of ((X) foo) Additionally, Kotlin performs smart casting where possible, so no additional cast needed after you check the type using is : open class Person : A() { val foo: Int = 42 } open class A and then: if (p is Person) { println(p.foo) // look, no cast

Reason to explicitly cast from char to int in Java?

谁说胖子不能爱 提交于 2020-08-11 17:57:48
问题 I have seen in Java code in many places, people tend to cast between primitives int and char. Is this necessary? Are they not implicitly converted. For e.g. I tried this and exactly got what I should. Then why do people explicitly cast? Am I missing something? char a = 'a'; int index = (int) a; index = 98; a = 98; System.out.println(index); System.out.println(a); 回答1: Sometimes people will cast for clarity, sometimes for overloading reasons, and sometimes for reasons of ignorance. For example

Pass generic type argument through the literal object with different field names per each field if number of fields values is limited by 4

断了今生、忘了曾经 提交于 2020-08-10 18:15:07
问题 What am I doing? I want to make mapping of an object field-by-field to objects of some generic type. I have 4 runtime cases (with runtime check and throwing exception if not matches) that I've used as a properties of original object: false transforms to Smth<unknown> | undefined true transforms to Smth<unknown> [] transforms to Smth<unknown> with 0 or more elements [true] transforms to Smth<unknown> with 1 or more elements Actually that's not very difficult, but I'm not very happy to have

How can we invoke the parent's method, when a child has a method with the same name in JavaScript? Anything like type casting in Java here? [duplicate]

做~自己de王妃 提交于 2020-08-10 01:24:37
问题 This question already has answers here : How to call a parent method from child class in javascript? (8 answers) Closed 10 days ago . class Parent { constructor(x) { this.x = x; } present() { return `I have a ${this.x}`; } } class Child extends Parent { constructor(x, y) { super(x); this.y = y; } present() { return `${super.present()}, it is a ${this.y}`; } } child = new Child("Tinggu", "Winggu"); console.log(child.present()); // invokes the child version How would I invoke the parent's

How can we invoke the parent's method, when a child has a method with the same name in JavaScript? Anything like type casting in Java here? [duplicate]

流过昼夜 提交于 2020-08-10 01:23:07
问题 This question already has answers here : How to call a parent method from child class in javascript? (8 answers) Closed 10 days ago . class Parent { constructor(x) { this.x = x; } present() { return `I have a ${this.x}`; } } class Child extends Parent { constructor(x, y) { super(x); this.y = y; } present() { return `${super.present()}, it is a ${this.y}`; } } child = new Child("Tinggu", "Winggu"); console.log(child.present()); // invokes the child version How would I invoke the parent's

I can call a function imported with dlsym() with a wrong signature, why?

故事扮演 提交于 2020-08-09 09:36:59
问题 host.cpp has: int main (void) { void * th = dlopen("./p1.so", RTLD_LAZY); void * fu = dlsym(th, "fu"); ((void(*)(int, const char*)) fu)(2, "rofl"); return 0; } And p1.cpp has: #include <iostream> extern "C" bool fu (float * lol) { std::cout << "fuuuuuuuu!!!\n"; return true; } (I intentionally left errors checks out) When executing host, “fuuuuuuuu!!!” is printed correctly, even though I typecasted the void pointer to the symbol with a completely different function signature. Why did this

Pandas error trying to convert string into integer

白昼怎懂夜的黑 提交于 2020-08-07 08:05:48
问题 Requirement : One particular column in a DataFrame is 'Mixed' Type. It can have values like "123456" or "ABC12345" . This dataframe is being written into an Excel using xlsxwriter . For values like "123456" , down the line Pandas converting it into 123456.0 ( Making it look like a float) We need to put it into xlsx as 123456 (i.e as +integer) in case value is FULLY numeric. Effort : Code Snippet shown below import pandas as pd import numpy as np import xlsxwriter import os import datetime

Cast vector of i8 to vector of u8 in Rust? [duplicate]

℡╲_俬逩灬. 提交于 2020-08-06 07:36:05
问题 This question already has answers here : How do I convert a Vec<T> to a Vec<U> without copying the vector? (2 answers) Closed 7 months ago . Is there a better way to cast Vec<i8> to Vec<u8> in Rust except for these two? creating a copy by mapping and casting every entry using std::transmute The (1) is slow, the (2) is "transmute should be the absolute last resort" according to the docs. A bit of background maybe: I'm getting a Vec<i8> from the unsafe gl::GetShaderInfoLog() call and want to

Type casting for Option type

北慕城南 提交于 2020-08-06 05:33:57
问题 I'm newbie in Rust from Python. I believe it's a basic question, but I am too new to find the answer by keywords like Type Casting Option . In Python, to make the type checker know return type is not Optional[int] + int , we can address assert logic to enforce the type checker know x will never be None after line assert . from typing import Optional def add_one(x: Optional[int] = None) -> int: if x is None: x = 0 assert x is not None return x + 1 if __name__ == '__main__': add_one(0) # 1 add