generics 相关问题

泛型是一种参数多态,可以在各种语言中找到,包括.NET语言,Java和Swift。

如何从父泛型类型引用子泛型类型?

这是场景 类 MyClass1{ } 类 MyClass2 扩展 MyClass1{ } 类父类{ 列表列表; } 子类扩展父类{ } 这不能编译 普...

回答 5 投票 0

MyGeneric1[T] 上的 GenericArguments[0]、TestClass 违反了类型参数“T”的约束

我正在尝试按照这篇博客文章创建一个扩展 ODataController 的通用控制器。 https://blog.scottlogic.com/2015/12/01/generalizing-odata.html 但我目前正在获得 Type e...

回答 1 投票 0

如何定义一个字典,将类型与接收该类型参数的可调用函数进行映射?

在 Python 中,我在定义类型时遇到困难。我想要这样的东西: def foo(x: int) -> int: 返回x def bar(x: str) -> str: 返回x # [...] MyDictType 将被定义...

回答 1 投票 0

在 SwiftUI 中复制 foregroundStyle(_:)

在 SwiftUI 中,foregroundStyle(_:) 似乎做了一些 @environment 魔法,但它也无法通过我能找到的任何 @Environment(\.insertMagicKeyHere) 键访问,所以我一直在尝试让我...

回答 1 投票 0

使用通用接收器时类型字符串和字符串不匹配[重复]

不要问我为什么这样做,只要告诉我这是怎么可能的: gopls 错误:类型字符串和字符串不匹配 类型 Mapsi2[T 字符串 |整数 |浮动32 | float64] 结构 { 键[]字符串 瓦尔...

回答 1 投票 0

如何使用通用存储库模式创建实体

我使用了通用存储库 接口 IGenericRepository 其中 T:class 数据访问方法之一是这种 post 方法 任务 AddEntityAsync(T Entity),我还创建了 add Dto 类...

回答 1 投票 0

如何处理动态结构实例化?

我有许多实现特征的结构,并且我需要一个函数根据运行时条件返回其中一个结构的实例。我也很难弄清楚签名...

回答 1 投票 0

Hibernate 6 和通用 JSON 字段

我正在使用 PostgreSQL 和 Hibernate 6,并且我有一个 JSONB 类型列 - 值。 问题是,如何为该字段定义具有通用类型的实体? 另外,有什么方法可以映射正确的

回答 1 投票 0

接口中的TypeScript通用方法签名

我正在尝试用一些方法定义一个接口,并且我希望其中一个方法是通用的。 它是一个filterUnique方法,所以它应该能够过滤数字、字符串等列表......

回答 2 投票 0

const 泛型和 `typenum` 箱子有重叠的目的吗?

在了解了 const 泛型并偶然发现了 typenum 箱子之后,我对它们似乎有重叠的目的这一事实感到困惑 - 使用数值作为类型。 唯一不同的是...

回答 1 投票 0


如何检查可变参数可调用的参数是否全部属于某个子类?

这可能是一项艰难的任务。 假设我有一个类型 JSON = Union[映射[str, "JSON"], 序列["JSON"], str, int, float, bool, None] 我有一个功能 def memoize[**P, T: JSO...

回答 1 投票 0

隐式打字稿按键映射使用

是否可以隐式声明以下示例中 name 属性的可能选项(基于键): 类型类型映射 = { 答:1 | 2 | 3; 乙:4 | 5 | 6; }; 类型 InnerType 是否可以在下面的示例中隐式说明 key 属性的可能选项(基于 name): type TypeMap = { A: 1 | 2 | 3; B: 4 | 5 | 6; }; type InnerType<TKey extends keyof TypeMap> = { key: TKey; name: TypeMap[TKey]; // + other properties; }; type OuterType = { x: InnerType; // Should be agnostic at this point y: InnerType; // but typescript requires a generic definition }; const foo: OuterType = { x: { key: "A", name: 2 }, y: { key: "B", name: 4 }, }; 我能想到的最接近的是如下所示,(但我宁愿不必更改相应类型的格式): type TypeMap = { A: 1 | 2 | 3; B: 4 | 5 | 6; }; type InnerType = { data: Partial<{ [key in keyof TypeMap]: TypeMap[key] }>; // + other properties }; type OuterType = { x: InnerType; y: InnerType; }; const foo: OuterType = { x: { data: { A: 2 } }, // not blocking me from using `{}` or `{ A: 2, B: 4 }` on the same object y: { data: { B: 4 } }, }; 另一种方法如下(但不基于密钥进行限制): type TypeMap = { A: 1 | 2 | 3; B: 4 | 5 | 6; }; type InnerType = { key: keyof TypeMap; name: TypeMap[keyof TypeMap]; // + other properties; }; type OuterType = { x: InnerType; y: InnerType; }; const foo: OuterType = { x: { key: "A", name: 2 }, // allows me to use `{ key: "A", name 4 }` y: { key: "B", name: 4 }, }; 您的 InnerType 版本是单个对象类型,其属性为 unions,它允许以您不希望的方式混合 key 和 name。您确实希望 InnerType 本身成为对象类型的联合,如下所示: type InnerType = { key: "A"; name: 1 | 2 | 3; } | { key: "B"; name: 4 | 5 | 6; } 这给了你你想要的行为: const foo: OuterType = { x: { key: "A", name: 1 }, y: { key: "B", name: 5 } }; const badFoo: OuterType = { x: { key: "A", name: 4 }, // error! // Type '{ key: "A"; name: 4; }' is not assignable to type 'InnerType'. // Type '4' is not assignable to type '1 | 2 | 3'. y: { key: "A", name: 2 } } 您甚至可以用 InnerType 来写 TypeMap,这样当 TypeMap 更新时它会自动更新: type InnerType = { [K in keyof TypeMap]: { key: K, name: TypeMap[K] } }[keyof TypeMap] 这个InnerType是在microsoft/TypeScript#47109中创造的分布式对象类型。它的工作原理是映射K的键TypeMap,计算每个键K所需的对象类型,然后使用keyof TypeMap索引到映射类型,从而得到所需的并集。 如果出于某种原因您想保留 InnerType generic 以便 InnerType<K> 对应于特定的 K constrained 到 keyof TypeMap,您可以这样做: type InnerType<K extends keyof TypeMap = keyof TypeMap> = { [P in K]: { key: P, name: TypeMap[P] } }[K] type InnerTypeA = InnerType<"A"> /* type InnerTypeA = { key: "A"; name: 1 | 2 | 3; } */ type InnerTypeB = InnerType<"B"> /* type InnerTypeB = { key: "B"; name: 4 | 5 | 6; } */ 并注意 K defaults 为 keyof TypeMap,因此没有泛型类型参数的 InnerType 相当于完整的联合: type InnerTypeBoth = InnerType /* type InnerTypeBoth = { key: "A"; name: 1 | 2 | 3; } | { key: "B"; name: 4 | 5 | 6; } */ 因此,您可以根据您的需要获得类似泛型和类似联合的行为。 Playground 代码链接

回答 1 投票 0

处理可选类成员的方法是什么?

处理大量数据(千兆字节)我使用数据数组的索引。由于访问数据可能会导致缓存效率低下,因此我想将数组中的一些数据与索引一起缓存......

回答 1 投票 0

为什么 Test<?> 的实例在构造函数中接受非空对象?

通配符?泛型中表示未知类型并且仅接受 null。 然而,在下面的示例中,构造函数接受(例如)一个 String 对象,即使我已经声明了一个

回答 1 投票 0

在 SwiftUI 中使用到集合的绑定

我正在尝试构建一个 TextField,它从集合中获取其值,就像 切换( _ titleKey: LocalizedStringKey, 来源:C, isOn:KeyPath 我正在尝试构建一个 TextField,它从集合中获取其值,就像 Toggle<C>( _ titleKey: LocalizedStringKey, sources: C, isOn: KeyPath<C.Element, Binding<Bool>> ) where C : RandomAccessCollection 我已构建以下内容: struct MultiTextField<C, V, F>: View where C: RandomAccessCollection & MutableCollection, F: ParseableFormatStyle, V == F.FormatInput, F.FormatOutput == String { let titleKey: LocalizedStringKey @Binding var sources: C let keyPath: KeyPath<Binding<C.Element>, Binding<V>> let format: F @State private var value: V? var body: some View { TextField(titleKey, value: $value, format: format) .onAppear(perform: { print($sources.projectedValue[keyPath: keyPath]) }) } } #Preview { struct Test { var value: Double } @State var sources = [Test(value: 0.1234), Test(value: 2.3456)] return MultiTextField(titleKey: "Test", sources: $sources, keyPath: \.value, format: .number) } 文本字段本身可以工作,但我需要将集合/keyPath 的结果与文本字段使用的值耦合。当文本字段被提交或失去焦点时,我想将值写回集合(如果它有效(!= nil))。 我不知道如何对集合和关键路径做任何有用的事情,因此我不知道如何继续这里。此处所示的 print 语句会产生编译错误: Key path of type 'KeyPath<Binding<C.Element>, Binding<V>>' cannot be applied to a base of type 'Binding<C>' 如果我将 keyPath 更改为 Binding<C> 的根类型,那么我会在预览中收到错误消息,指出 [Test] 没有动态成员 value。 我能想到的最好的办法是 keyPath 类型:WritableKeyPath<C.Element, V>。这允许我们在 sources 上执行映射并提取有用的值,但它不允许我实际将数组中的所有值更新为新值。 如何利用集合和关键路径? Toggle 如何访问其 sources? 你就快到了,只需做一些小改变。 import SwiftUI struct MultiTextField<C, V, F>: View where C: RandomAccessCollection & MutableCollection, C.Element : Identifiable, //Add Identifiable for the ForEach F: ParseableFormatStyle, V == F.FormatInput, F.FormatOutput == String { let titleKey: LocalizedStringKey @Binding var sources: C let keyPath: WritableKeyPath<C.Element, V> //Binding == read/write so you need WritableKeyPath, KeyPath is read-only. let format: F var body: some View { ForEach($sources, id:\.id){ $source in //C is a collection so you need a ForEach TextField(titleKey, value: Binding(get: { source[keyPath: keyPath] //CustomBinding }, set: { newValue in source[keyPath: keyPath] = newValue }), format: format) } } } #Preview { struct Test: Identifiable{ var id: UUID = .init() var value: Double } @State var sources = [Test(value: 0.1234), Test(value: 2.3456)] return MultiTextField(titleKey: "Test", sources: $sources, keyPath: \.value, format: .number) }

回答 1 投票 0

Dart:具有 .fromJson 构造函数的泛型

在 Swift 中,我习惯于设置一个协议 JSONInitialized,它定义了一个 init?(json: JSON),这意味着所有符合该协议的类和结构都可以使用 JSON 对象进行初始化...

回答 4 投票 0

Golang:在传递 func 作为参数时不能使用接口而不是结构

我已经实现了一个适用于切片的通用 Map 函数,并且我发现我的大型应用程序中有许多不同的结构,它们具有相同的 GetID 方法。 而且,我一直在寻找我的......

回答 1 投票 0

访问泛型结构体字段声明中的关联内容

我有一个标签特征,它有一个关联的常量: 特质 特质 { const N:使用; } 我有一个通用的类型,受特征限制。我希望能够在声明中使用 N: 结构...

回答 1 投票 0

如何构建封装的Toggle访问集合的关键路径

我正在尝试构建一个封装 Toggle 的新 SwiftUI 视图。 Toggle 访问集合,以便它可以以混合状态显示。 我在构建正确的 KeyPath 时遇到困难......

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.