我很好奇为什么隐式演员会失败...
int? someValue = SomeCondition ? ResultOfSomeCalc() : null;
Run Code Online (Sandbox Code Playgroud)
为什么我必须执行显式演员
int? someValue = SomeCondition ? ResultofSomeCalc() : (int?)null;
Run Code Online (Sandbox Code Playgroud)
在我看来,编译器具有进行隐式转换决策所需的所有信息,不是吗?
我正在使用本机C++创建数据库访问层,我正在寻找支持NULL值的方法.这是我到目前为止:
class CNullValue
{
public:
static CNullValue Null()
{
static CNullValue nv;
return nv;
}
};
template<class T>
class CNullableT
{
public:
CNullableT(CNullValue &v) : m_Value(T()), m_IsNull(true)
{
}
CNullableT(T value) : m_Value(value), m_IsNull(false)
{
}
bool IsNull()
{
return m_IsNull;
}
T GetValue()
{
return m_Value;
}
private:
T m_Value;
bool m_IsNull;
};
Run Code Online (Sandbox Code Playgroud)
这就是我必须定义函数的方法:
void StoredProc(int i, CNullableT<int> j)
{
...connect to database
...if j.IsNull pass null to database etc
}
Run Code Online (Sandbox Code Playgroud)
我称之为:
sp.StoredProc(1, 2);
Run Code Online (Sandbox Code Playgroud)
要么
sp.StoredProc(3, CNullValue::Null());
Run Code Online (Sandbox Code Playgroud)
我只是想知道是否有比这更好的方法.特别是我不喜欢CNullValue的单例类对象和静态.我更愿意这样做
sp.StoredProc(3, …Run Code Online (Sandbox Code Playgroud) 下面是一些代码,演示了我无法将结构类型声明和初始化为null.该可空类型是一个结构,所以为什么我可以将它设置为空?
Nullable<bool> b = null;
if (b.HasValue)
{
Console.WriteLine("HasValue == true");
}
//Does not compile...
Foo f = null;
if (f.HasValue)
{
Console.WriteLine("HasValue == true");
}
Run Code Online (Sandbox Code Playgroud)
在哪里Foo定义为
public struct Foo
{
private bool _hasValue;
private string _value;
public Foo(string value)
{
_hasValue = true;
_value = value;
}
public bool HasValue
{
get { return _hasValue; }
}
public string Value
{
get { return _value; }
}
}
Run Code Online (Sandbox Code Playgroud)
问题已得到解答(见下文).为了澄清我会发布一个例子.C#代码:
using System;
class Program
{
static …Run Code Online (Sandbox Code Playgroud) 为什么这个代码段的输出System.Int32代替Nullable<Int32>?
int? x = 5;
Console.WriteLine(x.GetType());
Run Code Online (Sandbox Code Playgroud) @RequestMapping(value = "/contact.html", method = RequestMethod.POST)
public final ModelAndView contact(
@RequestParam(value = "name", required = false) Optional<String> name) {
Run Code Online (Sandbox Code Playgroud)
如果不需要参数值且没有发送任何内容,Spring如何@RequestMapping处理Optional来自Guava库的内容?
那将会:
nullOptional.absent()可以Optional.fromNullable(T)用来接受请求吗?
您可能知道,DateTime?没有参数化ToString(为了格式化输出),并执行类似的操作
DateTime? dt = DateTime.Now;
string x;
if(dt != null)
x = dt.ToString("dd/MM/yyyy");
Run Code Online (Sandbox Code Playgroud)
会扔
方法'ToString'没有重载需要1个参数
但是,由于C#6.0和Elvis(?.)运算符,上面的代码可以替换为
x = dt?.ToString("dd/MM/yyyy");
Run Code Online (Sandbox Code Playgroud)
哪....有效!为什么?
请考虑以下代码:
int? x = null;
Console.Write ("Hashcode: ");
Console.WriteLine(x.GetHashCode());
Console.Write("Type: ");
Console.WriteLine(x.GetType());
Run Code Online (Sandbox Code Playgroud)
执行时,它会写入Hashcode 0,但NullReferenceException在尝试确定类型时失败x.我知道调用可空类型的方法实际上是在底层值上调用的,所以我希望程序在期间失败x.GetHashCode().
那么,这两种方法之间的根本区别是什么,为什么第一种方法失败呢?
我接下来有很多方法:
var result = command.ExecuteScalar() as Int32?;
if(result.HasValue)
{
return result.Value;
}
else
{
throw new Exception(); // just an example, in my code I throw my own exception
}
Run Code Online (Sandbox Code Playgroud)
我希望我可以使用这样的运算符??:
return command.ExecuteScalar() as Int32? ?? throw new Exception();
Run Code Online (Sandbox Code Playgroud)
但它会生成编译错误.
是否可以重写我的代码,或者只有一种方法可以做到这一点?
对于我自己实现的Equals()方法,我想检查一堆内部字段.我是这样做的:
...
_myNullableInt == obj._myNullableInt &&
_myString == obj._myString &&
...
Run Code Online (Sandbox Code Playgroud)
我假设,这比较值,包括null,对于相等而不是对象地址(作为参考euqality比较操作),因为:
对于此MSDN文档中的 "预定义值类型" ,可以这样说.我假设Nullable<int>是这样一个"预定义的值类型",因为System根据这个MSDN文档,它在命名空间中.
我是否正确地假设VALUES在这里进行比较?
注意:单元测试显示"是",但我希望其他人对这个问题感到放心,以防我错过了什么.
给定表1,其中一列为"x",类型为String.我想创建表2,其中列为"y",它是"x"中给出的日期字符串的整数表示形式.
必不可少的是将null值保留在"y"列中.
表1(数据帧df1):
+----------+
| x|
+----------+
|2015-09-12|
|2015-09-13|
| null|
| null|
+----------+
root
|-- x: string (nullable = true)
Run Code Online (Sandbox Code Playgroud)
表2(数据帧df2):
+----------+--------+
| x| y|
+----------+--------+
| null| null|
| null| null|
|2015-09-12|20150912|
|2015-09-13|20150913|
+----------+--------+
root
|-- x: string (nullable = true)
|-- y: integer (nullable = true)
Run Code Online (Sandbox Code Playgroud)
用于将列"x"中的值转换为列"y"的用户定义函数(udf)为:
val extractDateAsInt = udf[Int, String] (
(d:String) => d.substring(0, 10)
.filterNot( "-".toSet)
.toInt )
Run Code Online (Sandbox Code Playgroud)
并且工作,处理空值是不可能的.
尽管如此,我可以做类似的事情
val extractDateAsIntWithNull = udf[Int, String] (
(d:String) =>
if (d != …Run Code Online (Sandbox Code Playgroud) scala nullable user-defined-functions apache-spark apache-spark-sql
nullable ×10
c# ×7
.net ×5
apache-spark ×1
c#-6.0 ×1
c++ ×1
gettype ×1
guava ×1
optional ×1
reflection ×1
scala ×1
spring ×1
spring-mvc ×1
struct ×1
value-type ×1