我想将 int 值转换为可空枚举使用通用函数。我认为这很容易,尤其是所有关于 enum / int 转换的所有 SO。我发现的最接近的问题是this one,但不幸的是,它不处理可为空的枚举。 这个问题解决了 Nullable 枚举转换问题,但不能解决泛型问题。
这是我正在尝试做的一个简化示例:
public enum SouthParkCharacters
{
Stan = 0,
Kyle = 1,
Eric = 2,
Kenny = 3
}
public static T CastNullableEnum<T>(int value)
{
return (T)(object)((int?)value).Value;
}
public static T SomeFunction<T>(this int anyEnumValue)
{
SouthParkCharacters? spc = SouthParkCharacters.Kenny;
spc = CastNullableEnum<SouthParkCharacters>(3); //I am working!
spc = CastNullableEnum<SouthParkCharacters?>(3); //Raise error Specified cast is not valid.
//This function will be public so I won't have control over …Run Code Online (Sandbox Code Playgroud) 我有两个关于 Scala 中可为空类型的问题:
假设我希望定义一个新类:class myClass[T](x: T),并且我想确保它可以T为空。我怎么做?
我想编写一个函数def myFunc(x: T)(不作为上一个问题的一部分),如果可以T为空,我想执行一件事,否则执行另一件事。与上一个问题的不同之处在于,这里我不想限制T,而是知道它是否可以为空。我怎么做?
使用/或任何其他值类型作为我的 MVC 模型时,编译器似乎显示错误。structenum
CS0037 无法将 null 转换为“MyEnum”,因为它是不可为 null 的值类型
我创建了一个新的 MVC 项目(在 VS 2019 中),创建了一个枚举
public enum MyEnum
{
One,
Two,
Three
}
Run Code Online (Sandbox Code Playgroud)
并将其作为模型放在“关于”视图中:
为什么会发生这种情况?
I want to check if a type is nullable or not, and if it has a conditional type on the value.
I tried implementing
type IsNullable<T> = T extends null ? true : false;
Run Code Online (Sandbox Code Playgroud)
However, it does not seem to work
type test = IsNullable<number> // Returns false as it should
type test = IsNullable<number | null> // Returns false when it should be true
Run Code Online (Sandbox Code Playgroud)
What's the proper way of checking if a type is nullable? I tried with T …
我有以下代码:
string thing="";
if(request.Session.Attributes?.TryGetValue("varName", out thing))
{
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
request.Session.Attributes 是一个字典。
我知道你不能拥有if(bool?)上面所做的。我也知道您可以使用 .GetValueOrDefault() 以便将 null 视为 false。但我不能这样做如果 Attributes 是从 TryGetValue返回的,那么request.Session.Attributes?.GetValueOrDefault().TryGetValue("varName", out thing)
返回的正确方法false是什么?nullbool
我注意到 Dart/Flutter 标签上的更多用户尝试在较新的 Dart SDK 版本中尝试空安全,我开始阅读它,从这篇 Medium 文章开始。
我注意到在他们所有的例子中,他们都使用了位置性的、必需的参数。但是空安全如何与可选参数一起工作,包括位置参数和命名参数?
可选参数本质上是null这样的,这是否意味着所有可选参数都必须使用启用空安全的可空变量声明语法来声明?添加 似乎只是一个小小的不便?,但它可能会破坏大量使用可选参数的代码。dart 是否能够对可选参数进行例外处理(知道它们总是可以为空的),从而可以避免如此大的更改?或者是否有更简单的替代方法可以使我的代码与空安全兼容以避免这些更改?
我正在将一些 Java 应用程序移植到 Kotlin,但我一直在使用哈希图遇到这个问题。假设我有两个哈希图:
var firstHashmap: HashMap<String, String> = hashMapOf("foo" to "A", "bar" to "B")
var secondHashmap: HashMap<String, String> = hashMapOf("foo" to "C", "bar" to "D")
Run Code Online (Sandbox Code Playgroud)
如果另一个中存在相同的密钥,我想更新一个:
if (firstHashmap["foo"] != null) {
secondHashmap["foo"] = firstHashmap["foo"]
}
Run Code Online (Sandbox Code Playgroud)
Kotlin 不会编译第二行,因为firstHashmap不能保证有“foo”键。我只是在第一行检查它并不重要 - 智能转换系统显然不适用于 hashmap 键。这有同样的问题:
if (firstHashmap.containsKey("foo")) {
secondHashmap["foo"] = firstHashmap["foo"]
}
Run Code Online (Sandbox Code Playgroud)
这有效,但在某些情况下创建额外变量会变得混乱:
val newValue = firstHashmap["foo"]
if (newValue != null) {
secondHashmap["foo"] = newValue
}
Run Code Online (Sandbox Code Playgroud)
这也有效,但到目前为止我从未使用过!!修饰符:
if (firstHashmap["foo"] != null) {
secondHashmap["foo"] = firstHashmap["foo"]!!
}
Run Code Online (Sandbox Code Playgroud)
这行得通,但是 Elvis 运算符后面的默认值永远不会被使用,只是为了满足编译器,感觉不对: …
为什么这么放
Renderer? myRenderer;
Run Code Online (Sandbox Code Playgroud)
产生错误
The type 'Renderer' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable<T>'
Run Code Online (Sandbox Code Playgroud)
然而
Vector3? myVector3;
Run Code Online (Sandbox Code Playgroud)
才不是?
举例来说,我希望能够写
void ApplyMaterial(Material material)
{
Renderer? renderer = targetDefinedExternally;
renderer?.material = material;
}
Run Code Online (Sandbox Code Playgroud)
相比之下,以下不会产生错误
Vector3? vector;
float? magnitude = vector3?.magnitude;
vector3?.Normalize();
Run Code Online (Sandbox Code Playgroud)
这表明某些 Unity 类型可以为空,而其他类型则不能。我怎么知道哪个是哪个?(有没有一种优雅的方法来解决这个问题?)
(真糊涂)。
请假设我从 WCF 服务下载了一个访问:
type Visit = {
tservice : Nullable<DateTime>
}
Run Code Online (Sandbox Code Playgroud)
和一个由访问组成的访问数组。假设数组中的某些访问具有非空 tservice 而其他访问具有空 tservice 值,tservice 模式如何与 null 匹配?
即这失败了:,
let fillSchedule (v:Visits[]) =
v |> Array.iter ( fun f ->
match f.tservice with
| System.Nullable<DateTime> -> tservice IS null, do something
| _ -> tservice is NOT null, do something
)
Run Code Online (Sandbox Code Playgroud)
提前致谢。
我正在做一些研究,但无法找到答案(可能是因为我没有正确搜索)
考虑这段代码:
public function foo(?array $optionalParam);
Run Code Online (Sandbox Code Playgroud)
然后是这个:
public function foo(array $optionalParam = null);
Run Code Online (Sandbox Code Playgroud)
它们之间有什么区别?使用 PHPstorm 我注意到当我使用 时?,它会创建一个 PHPdoc 并将变量类型标记为type|null. 但是当我在没有那个参数的情况下调用函数时,PHP 在我脸上尖叫“你在开玩笑吗?$optionalParam 在哪里”。另一方面,我设法毫无问题地使用了该=null选项。
对不起,如果这个问题太简单了,但我没有在网上找到任何答案。