language-design

why can in TypeScript a possible number value in an interface be converted to a not possible number value in a class implementation?

丶灬走出姿态 提交于 2020-07-03 07:15:44
问题 Today I ran into an unexpected TypeScript compiler behaviour. I'm wondering if it's a bug or a feature. Probably it will be the last one, but then I would like to know the rationale behind it. If I declare an interface method with a parameter that can be a string | number , and create a class that implements that interface, then the class method can make that parameter only string . This leads to a situation where the class implementation is not expecting a number, but the compiler allows

Why are Java objects pointers to pointers?

流过昼夜 提交于 2020-03-18 06:56:11
问题 The JVMS says that: In some of Oracle’s implementations of the Java Virtual Machine, a reference to a class instance is a pointer to a handle that is itself a pair of pointers: one to a table containing the methods of the object and a pointer to the Class object that represents the type of the object, and the other to the memory allocated from the heap for the object data. I don't understand why references would be implemented this way rather than making them a pointer to the method table

Why are Java objects pointers to pointers?

与世无争的帅哥 提交于 2020-03-18 06:56:09
问题 The JVMS says that: In some of Oracle’s implementations of the Java Virtual Machine, a reference to a class instance is a pointer to a handle that is itself a pair of pointers: one to a table containing the methods of the object and a pointer to the Class object that represents the type of the object, and the other to the memory allocated from the heap for the object data. I don't understand why references would be implemented this way rather than making them a pointer to the method table

What is the intention of ODR?

非 Y 不嫁゛ 提交于 2020-03-14 05:23:15
问题 I do understand what ODR says, but I don't understand what it tries to achieve. I see two consequences of violating it - user will get syntax error, which is totally fine. And also there may be some fatal errors, and again the user would be the only one who's guilty. As example of violating ODR and getting some fatal error I imagine like this: a.cpp struct A { int a; double b; }; void f(A a) { std::cout << a.a << " " << a.b << std::endl; } main.cpp struct A { int a; int b; }; void f(A a); int

What is the intention of ODR?

泄露秘密 提交于 2020-03-14 05:20:06
问题 I do understand what ODR says, but I don't understand what it tries to achieve. I see two consequences of violating it - user will get syntax error, which is totally fine. And also there may be some fatal errors, and again the user would be the only one who's guilty. As example of violating ODR and getting some fatal error I imagine like this: a.cpp struct A { int a; double b; }; void f(A a) { std::cout << a.a << " " << a.b << std::endl; } main.cpp struct A { int a; int b; }; void f(A a); int

Why can't local variable be used in GNU C basic inline asm statements?

柔情痞子 提交于 2020-02-28 09:46:20
问题 Why cannot I use local variables from main to be used in basic asm inline? It is only allowed in extended asm, but why so? (I know local variables are on the stack after return address (and therefore cannot be used once the function return), but that should not be the reason to not use them) And example of basic asm: int a = 10; //global a int b = 20; //global b int result; int main() { asm ( "pusha\n\t" "movl a, %eax\n\t" "movl b, %ebx\n\t" "imull %ebx, %eax\n\t" "movl %eax, result\n\t"

Why pointer (*) and array ([]) symbols are bound to variable name and not to type in variable declaration?

会有一股神秘感。 提交于 2020-02-04 03:57:10
问题 There're a lot of questions on SO about details of pointer and array declarations in C (and C subset of C++). I'm more interested in why . Why do we have to put * , [] in front of every variable when we declare several pointers/arrays in a row? int *a, *b; int c[1], d[1]; Why do we have to type out things after/around variable names in function pointers? void (*foo_ptr)(int, int); Why do we have this feature that confuses a lot of newcomers, when even compilers recognize and report these

How to implement Swift-like enums with associated values in JavaScript?

爱⌒轻易说出口 提交于 2020-02-01 12:22:09
问题 The Swift language has a fantastic enum support. Not only can one define a standard enum with cases, but cases can have optional values "associated to them." For example, taken from the Swift docs: enum Barcode { case UPCA(Int, Int, Int, Int) case QRCode(String) case Other } Such that one could create a Barcode enum by passing in a value, like so: var productBarcode = Barcode.UPCA(8, 85909, 51226, 3) and also switch on productBarcode at a later date to retrieve the associated value (a tuple

How to implement Swift-like enums with associated values in JavaScript?

与世无争的帅哥 提交于 2020-02-01 12:17:06
问题 The Swift language has a fantastic enum support. Not only can one define a standard enum with cases, but cases can have optional values "associated to them." For example, taken from the Swift docs: enum Barcode { case UPCA(Int, Int, Int, Int) case QRCode(String) case Other } Such that one could create a Barcode enum by passing in a value, like so: var productBarcode = Barcode.UPCA(8, 85909, 51226, 3) and also switch on productBarcode at a later date to retrieve the associated value (a tuple

Why can't you have multiple interfaces in a bounded wildcard generic?

穿精又带淫゛_ 提交于 2020-01-26 11:23:48
问题 I know there's all sorts of counter-intuitive properties of Java's generic types. Here's one in particular that I don't understand, and which I'm hoping someone can explain to me. When specifying a type parameter for a class or interface, you can bound it so that it must implement multiple interfaces with public class Foo<T extends InterfaceA & InterfaceB> . However, if you're instantiating an actual object, this doesn't work anymore. List<? extends InterfaceA> is fine, but List<? extends