casting

Polymorphism and SwiftUI

谁说胖子不能爱 提交于 2020-05-23 21:40:26
问题 Given the following example: class ProfileTab: Identifiable { let id = UUID() let name: String init(name: String) { self.name = name } } class ProfileQuoteTab: ProfileTab { let answer: String let prompt: String init(name: String, answer: String, prompt: String) { self.answer = answer self.prompt = prompt super.init(name: name) } } class ProfileImageTab: ProfileTab { let image: Image init(name: String, image: Image) { self.image = image super.init(name: name) } } struct ContentView: View { let

Kotlin casting int to float

核能气质少年 提交于 2020-05-16 07:12:49
问题 I'm triying to learn Kotlin , and I just made a calculator program from console. I have functions to sum , divide etc. And when I try to cast to integers to float I get this error: Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Float The function is this: fun divide(a:Int,b:Int):Float{ return a as Float / b as Float; } What I'm doing wrong? 回答1: To confirm other answers, and correct what seems to be a common misunderstanding in Kotlin,

Kotlin casting int to float

风格不统一 提交于 2020-05-16 07:12:02
问题 I'm triying to learn Kotlin , and I just made a calculator program from console. I have functions to sum , divide etc. And when I try to cast to integers to float I get this error: Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Float The function is this: fun divide(a:Int,b:Int):Float{ return a as Float / b as Float; } What I'm doing wrong? 回答1: To confirm other answers, and correct what seems to be a common misunderstanding in Kotlin,

Overloading cast operator for enum class

荒凉一梦 提交于 2020-05-14 20:53:08
问题 In my project, I'm using multiple enum classes which I need to easily cast between depending on where I need to use them. They basically describe the same thing but are named differently to keep the code more easier to work with. Here are the enum classes: enum class tetroType { None, I, O, T, J, L, S, Z }; enum class gridBlock { Empty, Blue, Green, Orange, Purple, Red, Teal, Yellow }; Each value in tetroType corresponds to a value in gridBlock (f. e. tetroType::I = gridBlock::Teal), but the

laravel 5.6 id is automatically casted to int

你说的曾经没有我的故事 提交于 2020-05-14 19:28:29
问题 I'm using Laravel 5.6 with the following table structure: public function up() { Schema::create( 'traffic', function ( Blueprint $table ) { $table->string( 'id' ); $table->unsignedInteger( 'category_id' ); $table->unsignedInteger( 'magnitude_id' ); $table->unsignedInteger( 'start_id' )->nullable(); $table->unsignedInteger( 'destination_id' )->nullable(); $table->unsignedInteger('locale_id'); $table->integer( 'length' )->comment( 'in metres' ); $table->integer( 'delay' )->comment( 'in seconds'

How can a List<Long> references to an ArrayList which has BigInteger values

ぐ巨炮叔叔 提交于 2020-05-14 10:45:29
问题 This method is defined in a JpaRepository and runs a native PostgreSQL query. List<Long> distributorIds = distributorRepository .findDistributorIdsWithChildren(distributorId) It runs with no exception and on runtime I see BigInteger values in returned distributorIds ArrayList instead of Long values . It's same with this question: Bug in Spring Data JPA: Spring Data returns List<BigInteger> instead of List<Long> So how can this bug occur? I mean how JAVA allows this ? If Java doesn't check

How to reinterpret_cast in Swift?

左心房为你撑大大i 提交于 2020-05-14 03:57:53
问题 Our project depends on a C library that declares a general struct typedef struct { SomeType a_field; char payload[248]; } GeneralStruct and a more specific one: typedef struct { SomeType a_field; OtherType other_field; AnotherType another_file; YetAnotherType yet_another_field; } SpecificStruct We have some examples of its usage in C++ and in some cases it's needed to cast the general one to the specific one like: GeneralStruct generalStruct = // ... SpecificStruct specificStruct =

PostgreSQL: Create an index on timestamp::DATE [duplicate]

爷,独闯天下 提交于 2020-05-12 11:22:13
问题 This question already has answers here : PostgreSQL: Index the day part of a timestamp (2 answers) Closed 4 years ago . I am creating a summary table that sums up all events in a given day. INSERT INTO graph_6( day, event_type, (SELECT COUNT(*) FROM event e WHERE event_type = e.event_type AND creation_time::DATE = sq.day) FROM event_type CROSS JOIN (SELECT generate_series( (SELECT '2014-01-01'::DATE), (SELECT '2014-01-02'::DATE), '1 day') as day) sq; The creation_time column is indexed:

How do I cast a pointer to an int

感情迁移 提交于 2020-05-10 08:45:50
问题 I'm trying to store the value of an address in a non pointer int variable, when I try to convert it I get the compile error "invalid conversion from 'int*' to 'int'" this is the code I'm using: #include <cstdlib> #include <iostream> #include <vector> using namespace std; vector<int> test; int main() { int *ip; int pointervalue = 50; int thatvalue = 1; ip = &pointervalue; thatvalue = ip; cout << ip << endl; test.push_back(thatvalue); cout << test[0] << endl; return 0; } 回答1: int may not be

Scala Divide two integers and get a float result

纵然是瞬间 提交于 2020-04-29 06:22:17
问题 If I do the following println(3/4) >0 I would like to get a decimal answer instead of an integer. Because of the way I am printing in my actual code I would prefer to cast within the println if that is possible. 回答1: If you're typing the number, just type at least one of them as a float (I assume you mean Float not Double ): println(3f/4) If you already have the numbers in variables, convert at least one of them with toFloat val x = 3 println(x.toFloat/4) (In each case, if you have at least