在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?
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
据我了解,&[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 ×1
c++ ×1
c++20 ×1
concept ×1
python ×1
python-3.10 ×1
rust ×1
slice ×1
string ×1
type-hinting ×1