- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了以下代码以尝试提供类型安全的接口(interface):
namespace MWE
{
public abstract class C {}
public class A : C {}
public class B : C {}
public class Container<T> where T : C
{
public readonly T Value;
public static implicit operator T(Container<C> c)
{
return c.Value;
}
}
public interface IWrapper<out TC> where TC : C {}
public class Foo
{
public Foo(IWrapper<Container<C>> wrapper) {}
}
}
不幸的是,这无法编译。 Container<C>
-wrapper
的一部分Foo
的参数构造函数导致编译器产生以下错误:
The type 'MFE.Container<MFE.C>' cannot be used as type parameter 'TC' in the generic type or method 'IWrapper<TC>'. There is no implicit reference conversion from 'MFE.Container<MFE.C>' to 'MFE.C'.
The type 'MFE.Container<MFE.C>' must be convertible to 'WeirdTestStuff.C' in order to use it as parameter 'TC' in the generic interface 'MFE.IWrapper<out TC>'.
我无法弄清楚问题到底出在哪里,因为转换的协方差似乎存在,甚至还有来自 Container<T>
的隐式转换。至 T
定义。自 T : C
,我认为它应该像这样工作。
我想保留 Foo
的构造函数,如果可能的话。
我希望有人能指出我这个问题的解决方案
最佳答案
even an implicit conversion from a Container to T defined
确实如此,但这不是编译器所要求的。它需要:
implicit reference conversion
(我的重点)
隐式引用转换不是由任何用户定义的运算符提供的,并且仅当一种类型(直接或通过中间类型)派生自另一种类型时才被允许1。
Container
has-a C
并且可以通过用户定义的运算符转换为 C
但是这还不足以使其成为 C。您的问题太抽象了,无法说明此处的修复方法 - Container
是否应该是非通用的并派生自 C
?这是“关闭”编译器的明显方法,但可能无法解决您的实际问题。
您不能使用泛型使类型的基类型在运行时可设置。
1这些是 Eric Lippert 的 Representation-preserving conversions
关于c# - "no implicit reference conversion"即使定义了转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55276136/
这是我的查询: INSERT INTO location_province(name, country) SELECT child.name ,location_country.id
尽管我看到 C++ 教程中广泛使用术语 implicit casting 来表示这样一个事实,即当您将某种类型分配给另一种类型时,类型的转换将自动(隐式)完成,但我经常听说应该叫implicit co
所有表格都在 utf_unicode_ci 中。 我这样做是为了检查 SELECT table_schema, table_name, column_name, character_set_name,
def MyFun(result: ListBuffer[(String, DateTime, List[(String, Int)])]): String = { val json = (r
我刚刚在 Postgres 中创建了一个表,并收到一条通知消息,我不完全理解隐式索引和序列。如有任何澄清,我们将不胜感激。 my_database=# CREATE TABLE sites my_da
我正在关注 Fernando Villalobos 的 React.js - A guide for Rails developers AirPair 教程。 这里的目标是使用 Rails 和 Rea
当我选择一个选项时,我有通过多选列表在 dbase 中搜索的代码我有这个错误: Illegal mix of collations (utf8_general_ci,IMPLICIT) and (ut
我正在尝试使用 execl 调用来执行 kernel-space-driver (driver.c) 中的二进制文件此时(从第 850 行开始): if (!retval) {
我正在尝试在内核 3.13 上编译内核模块,但出现此错误: error: implicit declaration of function 'create_proc_read_entry' [-Wer
我检查了数据库表,发现它在 latin1_swedish_ci 中,所以我将其更改为 utf8_general_ci 然后我将排序规则从 latin1_swedish_ci 更改到所有字段的 utf8
尝试通过 MySQL 中的存储过程进行选择时出现以下错误 Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8mb4_ge
我收到了这个错误; Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8mb4_general_ci,IMPLICIT)
我需要您帮助确定为什么会出现此错误 Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT
我收到了这个错误; Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8mb4_general_ci,IMPLICIT)
MySql 上的错误信息: Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) fo
在我的删除服务器上执行 MySQL 中的存储过程时,如下所示: CREATE OR REPLACE PROCEDURE `SetNextPage`( IN `inRefNo` varchar(
我正在尝试为 Kali linux 2.0 安装我的 Alfa AWUS036ACH 适配器 我已经修复了之前的错误,但现在我被困在这里了。这是错误我正在接收。 os_dep/linux/rtw_an
我们正在使用以下存储过程,并且所有提到的表都使用“Collation = utf8_general_ci”,但我们仍然收到此错误: Error Code: 1267. Illegal mix of
我想让我的 User 表的 password 列在 mysql 中区分大小写。 表的说明如下: /*Table: mst_user*/ FIELD TYPE
我对这一切都很陌生,正在尝试在内核版本为 3.10.0-957.el7.x86_64 的虚拟机上编译程序。但我收到此错误: /home/../../../isr_demux.c: In functio
我是一名优秀的程序员,十分优秀!