协方差,逆变和不变性描述了现有类型继承层次结构在经历某些转换(例如泛型中的使用)时如何变化。如果转换保持原始层次结构的顺序,则它是“协变的”。如果它反转它,它是“逆变的”。如果它打破它,它是“不变的”。
根据我的理解,变体是一种类型系统构造,基于它,编译器可以纯粹从类型系统级别完成整个生命周期想法的大部分预期语义,如
不幸的是,Dart(根据设计)缺乏指定不变或逆变类型参数的能力。那么,当我需要他们时,我该如何解决他们缺席的情况呢? 以产品的抽象设置为例...
我正在尝试在Python中创建一个类似协变集合的类,如下所示: 从输入导入 Generic、TypeVar、List、cast 动物类(): 经过 狗类(动物): 经过 猫类(动物):...
TypeVar(A, B, covariant=True) 是什么意思?
今天我深入研究了里氏替换原理和协变/逆变。 我陷入了以下两者之间的区别: T = TypeVar("T", 边界=Union[A, B]) T = TypeVar(&q...
Python 静态类型提示/检查 Iterable[AnyStr] 与 Iterable[str] 之间的不匹配 |可迭代[字节]
我遇到了这个静态类型提示不匹配(与 Pyright): from __future__ 导入注释 输入 import AnyStr, Iterable def foo(i: Iterable[AnyStr]): 返回我 def bar(i: Iterab...
我有这样的代码: 类 RetInterface {...} 类 Ret1:公共 RetInterface {...} A类接口 { 民众: 虚拟 boost::shared_ptr get_r() const = 0; ... ...
我正在尝试将更派生的类型作为值添加到字典中<..., base>,但出现以下错误: 无法从 HandleIntegrationEvent 转换为 HandleIntegrationEvent 我正在尝试将更派生的类型作为值添加到字典中<..., base>但出现以下错误: 无法从 HandleIntegrationEvent<TR> 转换为 HandleIntegrationEvent<IBaseEvent> 示例 using System.Threading.Tasks; using System.Collections.Generic; public interface IBaseEvent { string Name { get; } } public class UserCreatedEvent: IBaseEvent { public string Name { get; } = "UserCreatedEvent"; } public delegate Task HandleIntegrationEvent<in TR>(TR @event) where TR: IBaseEvent; public class IntegrationBus { private readonly IDictionary<string, HandleIntegrationEvent<IBaseEvent>> _listeners = new Dictionary<string, HandleIntegrationEvent<IBaseEvent>>(); public void RegisterEventListener<TR>(string @event, HandleIntegrationEvent<TR> listener) where TR: IBaseEvent { // ERROR: cannot convert from HandleIntegrationEvent<TR> to HandleIntegrationEvent<IBaseEvent> _listeners.Add(@event, listener); } } 我只是无法理解它,并且已经尝试理解这个问题有一段时间了。 据我了解,通用约束应该确保实例已实现 IBaseEvent 接口。 我只是暂时有心理障碍 编辑 发现另一篇很棒的文章https://stackoverflow.com/a/12841831解释了字典协方差和逆变的原因。 但我还是有点迷茫 您需要更改字典以存储Func<IBaseEvent, Task>而不是HandleIntegrationEvent<IBaseEvent>。然后,在添加到字典时,用 Func<IBaseEvent, Task> 包装侦听器以将事件转换为适当的类型。 更新代码: private readonly IDictionary<string, Func<IBaseEvent, Task>> _listeners = new Dictionary<string, Func<IBaseEvent, Task>>(); //add using System; if you get an Func<> error public void RegisterEventListener<TR>(string @event, HandleIntegrationEvent<TR> listener) where TR : IBaseEvent { _listeners.Add(@event, (e) => listener((TR)e)); }
为什么是[1 | 2] 不是 [1 | 的子类型[]] | [2 | []] 在 TypeScript 中?
在 TypeScript 中,我有一个 API,其输入是元组的并集,并且我有一个值,其类型是我想要传递给它的并集的元组。似乎这在大多数情况下都有效,但我无法想象......
PEP 484 声明 Typing.Type[T] 是协变的,即对于任何类型 A 和 B,B 是 A 的子类型意味着 Type[B] 是 Type[A] 的子类型,这显然是错误的: >>> 来自...
为什么 C# 中返回类型为 IList<List<T>> 时无法返回 List<IList<T>>?
我正在尝试在返回类型为 IList> 的方法中返回 List>: IList> Foo() { 返回新列表 我正在尝试在返回类型为 IList> 的方法中返回 List>: IList<IList<string>> Foo() { return new List<List<string>>(); } 但是,我收到编译错误,提示我无法将 List> 转换为 IList>。我认为既然 List 实现了 IList,我应该能够返回一个 List>,其中需要一个 IList>。 我已经向副驾驶询问过这个问题,并得到了关于 IList 不是协变或逆变的答案,但我仍然无法理解它。 List> 的返回类型是有效的,这让我很困惑。 通用接口协变意味着我们可以用与 T 相同或更派生的类型来替代 T。 IList<T> 不是协变的。 IEnumerable<out T> 是。 例如,这会起作用,因为 IEnumerable 通用接口是协变的: IEnumerable<IList<string>> Foo() { return new List<List<string>>(); } 但是,使用 IList<T>,您需要替换完全相同的 T。
PyTorch 的 _PositiveDefinite() 检查在整批 2x2 协方差矩阵上失败,但在所有单个矩阵上成功?
我想知道是否有 PyTorch 舍入误差或 2x2 协方差矩阵的 PositiveDefinite 检查经验可以帮助我更好地理解这个问题? 错误是…… 例外有
倾向评分匹配中的混杂变量 - 平衡变量的治疗和控制之间的标准化均值差 > 0.1
这是一个与因果推理相关的问题,特别是关于如何处理不平衡变量。我应用了 XGBoost 模型来为用户创建倾向得分(发现 XGBoost 具有更高的准确度...
我正在寻找在股票数据上创建方差/协方差矩阵或相关矩阵(我不在乎哪个)。 问题是不同的交易品种在不同的时间进行交易,所以我的数据框不适合...
我有一个形状为 (t, n1, n2) 的 3D numpy 数组: x = np.random.rand(10, 2, 4) 我需要计算另一个形状为 (t, n1, n1) 的 3D 数组 y ,使得: y[0] = np.cov(x[0,:,:]) ...等等...
如何为具有协变返回类型的方法的类型层次结构编写通用扩展方法。 例如,以下类型: 密封接口 MyInterface { 有趣的地图():MyInterf...
在此主题中 如何获取 null 而不是 KeyNotFoundException 通过键访问字典值? 在我自己的答案中,我使用显式接口实现来更改基本字典索引...
假设我有接口和类: 公共接口 ITree {} 公共类树:ITree {} 由于 IEnumerable 是协变的,因此下面的代码行编译成功: IE可枚举 假设我有接口和类: public interface ITree {} public class Tree : ITree {} 由于IEnumerable<T>是协变,所以下面的代码行编译成功: IEnumerable<ITree> trees = new List<Tree>(); 但是当我将它放入泛型方法中时: public void Do<T>() where T : ITree { IEnumerable<ITree> trees = new List<T>(); } 我从编译器中得到编译错误: 错误1无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.IEnumerable”。存在显式转换(是否缺少转换?) D:\lab\Lab.General\Lab.General\Program.cs 83 40 Lab.General 为什么协方差在这种情况下不起作用? 这是因为方差仅适用于引用类型(类、接口和委托)。添加一个类约束,它就可以正常编译了: public static void Do<T>() where T : class, ITree 我只是想添加更多示例: 您可以调用 MakeDesignWhereTIsReference 并将 VehicleStruct 传递为 T。但问题是: 引用类型支持协变和逆变,但值类型不支持它们。来自MS 文档 所以我们还需要限制T为引用类型;这可以借助 class 关键字来完成,该关键字添加了我们需要的限制。 public static class TestCovariance { public static void MakeDesignWhereTIsReference<T>() where T : class, IVehicle // pay attention to class restriction { ICollection<IDesign<IVehicle>> list = new List<IDesign<IVehicle>>(); var design = new Design<T>(); list.Add(design); } public static void MakeDesignWhereTIsStruct<T>() where T : struct, IVehicle //This method will throw a cast exception { ICollection<IDesign<IVehicle>> list = new List<IDesign<IVehicle>>(); var design = new Design<T>(); list.Add((IDesign<IVehicle>)design); } } public struct VehicleStruct : IVehicle { } TestCovariance.MakeDesignWhereTIsReference<Ship>(); TestCovariance.MakeDesignWhereTIsStruct<VehicleStruct>();
将 numpy 导入为 np def cov(x,y): 如果 len(x) != len(y): 返回 a_mean = np.mean(x) b_mean = np.mean(y) 总和 = 0 对于范围内的 i(0, len(x)): 总和 += ((x[i] - a_mean)...