language-design

Why does Python `**` use for exponentiation instead of the `^` operator? [duplicate]

假如想象 提交于 2021-02-04 21:59:30
问题 This question already has answers here : What do these operators mean (** , ^ , %, //)? [closed] (3 answers) Closed 2 years ago . Why is ^ not squaring in Python? I know exponentiation is ** instead, but what exactly is ^ and why wasn't that operator used instead? For example 2^2=0 , 3^2=1 . 回答1: The ^ operator was already used for bitwise xor. >>> x = 42; format(x, '08b') '00101010' >>> y = 137; format(y, '08b') '10001001' >>> z = x ^ y; format(z, '08b') '10100011' That leaves the old

Why does Python `**` use for exponentiation instead of the `^` operator? [duplicate]

前提是你 提交于 2021-02-04 21:58:30
问题 This question already has answers here : What do these operators mean (** , ^ , %, //)? [closed] (3 answers) Closed 2 years ago . Why is ^ not squaring in Python? I know exponentiation is ** instead, but what exactly is ^ and why wasn't that operator used instead? For example 2^2=0 , 3^2=1 . 回答1: The ^ operator was already used for bitwise xor. >>> x = 42; format(x, '08b') '00101010' >>> y = 137; format(y, '08b') '10001001' >>> z = x ^ y; format(z, '08b') '10100011' That leaves the old

Why doesn't Rust support trait object upcasting?

空扰寡人 提交于 2021-01-28 05:36:17
问题 Given this code: trait Base { fn a(&self); fn b(&self); fn c(&self); fn d(&self); } trait Derived : Base { fn e(&self); fn f(&self); fn g(&self); } struct S; impl Derived for S { fn e(&self) {} fn f(&self) {} fn g(&self) {} } impl Base for S { fn a(&self) {} fn b(&self) {} fn c(&self) {} fn d(&self) {} } Unfortunately, I cannot cast &Derived to &Base : fn example(v: &Derived) { v as &Base; } error[E0605]: non-primitive cast: `&Derived` as `&Base` --> src/main.rs:30:5 | 30 | v as &Base; | ^^^^

Why design a language with unique anonymous types?

纵然是瞬间 提交于 2020-11-25 07:27:38
问题 This is something that has always been bugging me as a feature of C++ lambda expressions: The type of a C++ lambda expression is unique and anonymous, I simply cannot write it down. Even if I create two lambdas that are syntactically exactly the same, the resulting types are defined to be distinct. The consequence is, that a) lambdas can only be passed to template functions that allow the compile time, unspeakable type to be passed along with the object, and b) that lambdas are only useful

Why design a language with unique anonymous types?

倖福魔咒の 提交于 2020-11-25 07:26:43
问题 This is something that has always been bugging me as a feature of C++ lambda expressions: The type of a C++ lambda expression is unique and anonymous, I simply cannot write it down. Even if I create two lambdas that are syntactically exactly the same, the resulting types are defined to be distinct. The consequence is, that a) lambdas can only be passed to template functions that allow the compile time, unspeakable type to be passed along with the object, and b) that lambdas are only useful

Why design a language with unique anonymous types?

别来无恙 提交于 2020-11-25 07:26:28
问题 This is something that has always been bugging me as a feature of C++ lambda expressions: The type of a C++ lambda expression is unique and anonymous, I simply cannot write it down. Even if I create two lambdas that are syntactically exactly the same, the resulting types are defined to be distinct. The consequence is, that a) lambdas can only be passed to template functions that allow the compile time, unspeakable type to be passed along with the object, and b) that lambdas are only useful

What can ref do that references couldn't?

99封情书 提交于 2020-08-05 07:11:21
问题 What can ref do that references couldn't? Could match value.try_thing() { &Some(ref e) => do_stuff(e), // ... } not be equally expressed by match value.try_thing() { &Some(e) => do_stuff(&e), // ... } 回答1: No, it is not avoidable with your proposed syntax. Your syntax does not allow for taking a reference when otherwise a move would be permissable. In this example, inner is a copy of the integer from val and changing it has no effect on val : fn main() { let mut val = Some(42); if let &mut