types

Check if two generic types are equal

微笑、不失礼 提交于 2021-02-18 22:31:28
问题 I need to find if a type is a certain generic type. class MyType<T> {} var instance = new MyType<int>(); var type = instance.GetType(); This check does not work, but this is what I want to check. If the type is of that generic type, regardless of what T is. type == typeof( MyType<> ) This does work, but feels dirty. It could also be wrong since it's not the FullName . type.Name == typeof( MyType<> ).Name I'm assuming there is a way to do this, but I haven't found one. Using IsAssignableFrom

How to recursively Omit key from type

只愿长相守 提交于 2021-02-18 12:51:16
问题 I want to write a type utility that omits fields recursively. Something that you would name and use like that OmitRecursively<SomeType, 'keyToOmit'> I've tried to do it using mapped types + conditional typing but I stuck on the case when all required fields got typed correctly (hence field disappeared from nested type), but optional fields are ignored with that approach. // This is for one function that removes recursively __typename field // that Appolo client adds type Deapolify<T extends {

C# float trouble. Why 1000000 + 0.10f = 1000000?

喜你入骨 提交于 2021-02-17 06:20:14
问题 Why Console.WriteLine((1000000f + 0.10f).ToString("N2")); print 1 000 000.00 but no 1 000 000.10? When I use type "double" or type "float" less 1000000 - this problem disappears! 回答1: Use decimal to prevent those accuracy/rounding issues. Console.WriteLine((1000000m + 0.10m).ToString("N2")); Reason: float has only a accuracy of 7 digits (reference) - your number has 8 回答2: According to MSDN, float type has only 7 digits precision. One milion has 7 digits, decimal part is rounded. Double type

When do you need to use type annotations?

雨燕双飞 提交于 2021-02-16 21:34:12
问题 While reading Data Types from the Rust Book I noticed that sometimes a variable is defined with a type annotation and sometimes not. When should I use type annotations? let tup: (i32, f64, u8) = (500, 6.4, 1); let tup = (500, 6.4, 1); let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; let a: [i32; 5] = [1, 2, 3, 4, 5]; 回答1: When types have to be specified If the compiler cannot infer the type by itself, it

When do you need to use type annotations?

ぃ、小莉子 提交于 2021-02-16 21:33:52
问题 While reading Data Types from the Rust Book I noticed that sometimes a variable is defined with a type annotation and sometimes not. When should I use type annotations? let tup: (i32, f64, u8) = (500, 6.4, 1); let tup = (500, 6.4, 1); let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; let a: [i32; 5] = [1, 2, 3, 4, 5]; 回答1: When types have to be specified If the compiler cannot infer the type by itself, it

typeid.name() not changing when iterating through a vector. Dynamic cast and typeid a base class pointer

Deadly 提交于 2021-02-16 21:06:58
问题 Answer: In short use virtual functions! So don't actually use this as good design, but for learning purposes take a read! I want to start off by saying I am using c++ and Qt I have a vector of Shape pointers (Base class) Edit: doSomething() is not a member of the Base class but instead a derived class member. Which is why I am using dynamic_cast to get Shape* to the Derived* so that I can access it. I am really doing this just out of curiosity at this point though and for other peoples

typeid.name() not changing when iterating through a vector. Dynamic cast and typeid a base class pointer

折月煮酒 提交于 2021-02-16 21:04:54
问题 Answer: In short use virtual functions! So don't actually use this as good design, but for learning purposes take a read! I want to start off by saying I am using c++ and Qt I have a vector of Shape pointers (Base class) Edit: doSomething() is not a member of the Base class but instead a derived class member. Which is why I am using dynamic_cast to get Shape* to the Derived* so that I can access it. I am really doing this just out of curiosity at this point though and for other peoples

How to return concrete type from generic function?

天大地大妈咪最大 提交于 2021-02-16 15:29:06
问题 In the example below the Default trait is used just for demonstration purposes. My questions are: What is the difference between the declarations of f() and g() ? Why g() doesn't compile since it's identical to f() ? How can I return a concrete type out of a impl trait generically typed declaration? struct Something { } impl Default for Something { fn default() -> Self { Something{} } } // This compiles. pub fn f() -> impl Default { Something{} } // This doesn't. pub fn g<T: Default>() -> T {

Cpp uint32_fast_t resolves to uint64_t but is slower for nearly all operations than a uint32_t (x86_64). Why does it resolve to uint64_t?

久未见 提交于 2021-02-16 13:58:29
问题 Ran a benchmark and uint32_fast_t is 8 byte but slower than 4 byte uint32_t for nearly all operations. If this is the case why does uint32_fast_t not stay as 4 bytes? OS info: 5.3.0-62-generic #56~18.04.1-Ubuntu SMP Wed Jun 24 16:17:03 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux Cpu info: cat /sys/devices/cpu/caps/pmu_name skylake model name : Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz Benchmark I used for testing: #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <cstdint>

Is it possible to declare a typescript function which works on both numbers and bigints?

a 夏天 提交于 2021-02-16 08:43:52
问题 In plain untyped Javascript, it's not so hard to write a function which can operate on either numbers or bigints, depending on the arguments which are passed in: const sumOfSquares = (a,b) => a*a + b*b; sumOfSquares(3, 4); // returns 25 sumOfSquares(3n, 4n); // returns 25n sumOfSquares(3n, 4); // throws a TypeError It seems like there ought to be a way to declare this function in typescript so that the compiler will enforce that the arguments will work together. I tried const sumOfSquares =