小编ggh*_*voc的帖子

新的联合速记表示“| 不支持的操作数类型:'str' 和 'type'”

在3.10之前,我使用的Union是创建union参数注释:

from typing import Union

class Vector:
    def __mul__(self, other: Union["Vector", float]):
        pass
Run Code Online (Sandbox Code Playgroud)

现在,当我使用新的 union 速记语法时:

class Vector:
    def __mul__(self, other: "Vector" | float):
        pass
Run Code Online (Sandbox Code Playgroud)

我收到错误:

TypeError: unsupported operand type(s) for |: 'str' and 'type'

Is this not supported?

python type-hinting python-typing python-3.10

24
推荐指数
1
解决办法
4399
查看次数

C++ Concepts Compound Requirements without noexcept and return-type-requirement vs Simple Requirements

template<class T>
concept C1 = requires(T a, T b) { a + b; };

template<class T>
concept C2 = requires(T a, T b) { { a + b }; };

Run Code Online (Sandbox Code Playgroud)

Would there be a difference between C1 vs C2 functionally?

Edit: grammar

c++ concept c++20

2
推荐指数
1
解决办法
67
查看次数

在 Rust 中,为什么 '[T]' 和 'str' 切片类型不是语法错误?

据我了解,&[T]类型是一个“胖”指针(内存地址和大小)而不是切片本身[T],而是被引用的实际切片。但是为什么他们没有[T]在下面的上下文中犯语法错误呢?

let y: [i32; 6] = [1, 2, 3, 4, 5, 6];

// let z: [i32] = y[..]; // error: the size for values of type `[i32]` cannot be known at compilation time

let z: &[i32] = &y[..]; // OK

// let v: str = "Hello World"; // the size for values of type `str` cannot be known at compilation time
Run Code Online (Sandbox Code Playgroud)

从错误:[i32] cannot be known at compilation,这只是一个错误,让我,用户,理解为什么这种语法是不可能的,还是因为我没有正确使用这种语法并且它在某些上下文中有效?

编辑:修正错别字

arrays string slice rust

0
推荐指数
1
解决办法
73
查看次数