- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我倾向于厌恶代码中的重复,所以当我遇到唯一不同的是类型的问题时,我倾向于使用泛型。来自 C++ 背景,我发现 vb.net 的版本相当令人沮丧,我知道 C++ 有模板特化,我猜 vb.net 没有所以我有一组例程,无论传递的类型如何,它们都执行完全相同的代码。
像这样
Public Sub decision(Of T)(ByVal a As T, ByVal b As Integer)
If b > 10 then
gt(a)
Else
lt(a)
End If
End Sub
我只将两种类型传递给例程,字符串和整数,这些例程对字符串的处理与对整数的处理不同。
Public Sub gt(ByVal a As String)
Public Sub gt(ByVal a As Integer)
Public Sub lt(ByVal a As String)
Public Sub lt(ByVal a As Integer)
这就是我对 vb.net vs C++ 感到沮丧的地方,据我所知,C++ 会在编译时检查类型,并且只针对发送给决策的类型进行检查。但是,在 vb.net 中我得到一个错误,类型 T 无法转换为字符串或整数
Error 3 Overload resolution failed because no accessible 'gt' can be called with these arguments:
'Public Sub gt(a As String)': Value of type 'T' cannot be converted to 'String'.
'Public Sub gt(a As Integer)': Value of type 'T' cannot be converted to 'Integer'.
我尝试了约束 Public Sub decision(Of T As {String, Integer})(ByVal a As T, ByVal b As Integer)
但约束必须是可继承的类,因此既不是字符串也不是整数可以使用。
我的下一个解决方案是添加 gt
和 lt
的通用版本:
Public Sub lt(Of T)(ByVal a As T)
Debug.Fail("Not Implemented")
End Sub
Public Sub lt(Of T)(ByVal a As T)
Debug.Fail("Not Implemented")
End Sub
嘿!没有更多的编译错误,但是唯一被调用的例程是 gt
和 lt
的通用版本。鉴于之前的 cannot convert 错误,我想这是有道理的。我以前遇到过这个问题,那里有泛型例程的非泛型重载,当时我找不到解决方案,现在也找不到解决方案。
是否有什么我遗漏的东西会使这种类型的重载成为可能?
编辑:一个完整的工作示例
Module Module1
Sub Main()
decision(1, 5)
decision(1, 10)
decision("hello world", 5)
decision("hello world", 10)
End Sub
Public Sub decision(Of T)(ByVal a As T, ByVal b As Integer)
If b > 10 Then
gt(a)
Else
lt(a)
End If
End Sub
Public Sub gt(ByVal a As String)
Debug.WriteLine(" gt string: " + a)
End Sub
Public Sub gt(ByVal a As Integer)
Debug.WriteLine(" gt integer: " + a.ToString)
End Sub
Public Sub lt(ByVal a As String)
Debug.WriteLine(" lt string: " + a)
End Sub
Public Sub lt(ByVal a As Integer)
Debug.WriteLine(" lt integer: " + a.ToString)
End Sub
#If False Then
Public Sub gt(Of T)(ByVal a As T)
Debug.Fail("Not implemented")
End Sub
Public Sub lt(Of T)(ByVal a As T)
Debug.Fail("Not implemented")
End Sub
#End If
End Module
最佳答案
来自 Differences Between C++ Templates and C# Generics (同样适用于 VB .NET):
C++ allows code that might not be valid for all type parameters in the template, which is then checked for the specific type used as the type parameter. C# requires code in a class to be written in such a way that it will work with any type that satisfies the constraints. For example, in C++ it is possible to write a function that uses the arithmetic operators + and - on objects of the type parameter, which will produce an error at the time of instantiation of the template with a type that does not support these operators. C# disallows this; the only language constructs allowed are those that can be deduced from the constraints.
我无法使用 .NET 泛型解决您的问题。但是您可以通过使用 lambda 和闭包来避免重复逻辑,我也认为这是在 .NET 中执行此操作的更自然的方法:
Public Sub Decision(ByVal a As String, ByVal b As Integer)
Decision(b, Sub() gt(a), Sub() lt(a))
End Sub
Public Sub Decision(ByVal a As Integer, ByVal b As Integer)
Decision(b, Sub() gt(a), Sub() lt(a))
End Sub
Private Sub decision(ByVal b As Integer, ByVal gt As Action, ByVal lt As Action)
If b > 10 Then
gt()
Else
lt()
End If
End Sub
Public Sub gt(ByVal a As String)
Debug.WriteLine(" gt string: " + a)
End Sub
Public Sub gt(ByVal a As Integer)
Debug.WriteLine(" gt integer: " + a.ToString)
End Sub
Public Sub lt(ByVal a As String)
Debug.WriteLine(" lt string: " + a)
End Sub
Public Sub lt(ByVal a As Integer)
Debug.WriteLine(" lt integer: " + a.ToString)
End Sub
关于vb.net 专用/重载泛型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9023915/
我试图让通用链接在我的应用程序中作为 URL 方案的替代品工作,因为从 iOS 9 开始,系统看起来每次从 Safari 调用应用程序时都会显示一个警告对话框。我希望在使用通用链接时替换关联域中注册
对于给定的常量Map val ctt = Map("a" -> 1, "b" -> 2) 如何定义一个String插值器c在哪里 c"a" 交付List(1)? 注意已考虑String Interpo
最近,我强化了我的 Keycloak 部署以使用专用的 Infinispan 集群作为 remote-store为 Keycloak 的各种缓存提供额外的持久层。更改本身进行得相当顺利,尽管在进行此更
我有一个非常明确的问题: //.h file @property (nonatomic, retain)NSMutableString * retainString; @property (nonat
我倾向于厌恶代码中的重复,所以当我遇到唯一不同的是类型的问题时,我倾向于使用泛型。来自 C++ 背景,我发现 vb.net 的版本相当令人沮丧,我知道 C++ 有模板特化,我猜 vb.net 没有所以
问题:如何确认我的“专属服务器”是否正常运行? 背景:我正在努力获得在独立 Linux 系统上运行的“专用 CoreNLP 服务器”。该系统是运行 CentOS 7 的笔记本电脑。选择该操作系统是因为
我一直在痛苦地寻找公平的电话,说 Clouding 把这个和那个给了管理层。到目前为止,无论我在同一个专用/或共享网络托管中找到什么,但云计算如何在软件行业产生巨大影响? 我一直在寻找关于什么是云托管
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 5 年前。 此帖子已于
我没有网络背景。我是数据平台解决方案架构和数据/人工智能工程师。由于分布式数据处理和使用容器进行横向扩展已经进入我的世界,我决定硬着头皮学习 IP 基础知识,以便我可以构建集群。 我一直在做大量的培训
我是 Prototype 的长期用户,总体上非常满意。我最近改用 jQuery 是因为得到了庞大的社区支持,基本上可以说是一致的选择和事实上的行业标准。从那以后我就没那么开心了。是的,我已经阅读了比较
我的应用程序使用各种繁重的库和数据文件,需要在启动时加载/与本地存储同步。由于此操作需要一些时间,因此我正在考虑为此目的创建一个专门的 Activity 。此 Activity 将是应用程序的入口点,
我有一个正在开发的 C++ 程序,它处理许多不同类型的对象,这些对象需要在启动时从文件中检索。总文件大小略小于 1GB。该程序必须至少在 Windows 7 和 Linux 上运行。 我的目标是提供读
我正在为我的页面制作适合移动设备的样式表。有没有一种简单的方法让它向 iPhone/Android 用户显示该样式表?或者我是否必须拉出用户代理并以这种方式解决 - 我该怎么做? 还有 - 有什么工具
我发现各种元素非常困惑。几乎每个元素似乎都有一个与之关联的“部分”,我不确定它们是如何粘合在一起的。 工作簿工作簿部分工作表工作表部分 我也对 DocumentFormat.OpenXml.Packa
我有一个由多个 Zend Framework 应用程序组成的服务器。 我想知道将 Zend Library 上传到服务器并在所有应用程序之间共享而不是按应用程序上传是否是个好主意。 例如,如果多个应用
我们有一个简单的表,需要转换为 XML Declare @Person TABLE ( [BusinessEntityID] [int] NOT NULL, [PersonType] [
当我重新部署容器组时,私有(private) IP 地址发生变化。它通常是三个地址之一。因此,我认识到我的负载均衡器只能拥有所有这三个名称,并且似乎知道哪一个当前实际上处于事件状态,但我更希望能够让负
我目前正在尝试通过我的私有(private) aks 集群部署我的 Helm Chart。但是,我无法执行任何操作,因为它找不到本地目录的路径。 这是我正在运行的命令: az aks command
我已为 Azure 存储帐户设置专用终结点。现在,这一切都为我创建了专用端点和专用链接。此外,我还可以直接从同一事件中的虚拟机访问我的存储帐户(storageaccountA)。 现在,从我的虚拟机进
我们的组织拥有 Azure Synapse 专用池实例。我正在尝试向 Azure Purview 注册 Azure Synapse 专用池并想要扫描 Synapse DB。但是,我每次都会收到以下错误
我是一名优秀的程序员,十分优秀!