我有一个枚举
string name;
public enum Color
{
Red,
Green,
Yellow
}
Run Code Online (Sandbox Code Playgroud)
如何在加载时将其设置为NULL.
name = "";
Color color = null; //error
Run Code Online (Sandbox Code Playgroud)
编辑:我的不好,我没有正确解释.但所有与可空的相关的答案都是完美的.我的情况是如果,我已经在类中使用其他元素(例如名称等)获取/设置枚举.在页面加载时,我初始化类并尝试将值默认为null.这是场景(代码在C#中):
namespace Testing
{
public enum ValidColors
{
Red,
Green,
Yellow
}
public class EnumTest
{
private string name;
private ValidColors myColor;
public string Name
{
get { return name; }
set { name = value; }
}
public ValidColors MyColor
{
get { return myColor; }
set { myColor = value; }
}
}
public partial class _Default : …Run Code Online (Sandbox Code Playgroud) 我用unsigned创建了一个迁移user_id.如何user_id在新迁移中编辑以进行编辑nullable()?
Schema::create('throttle', function(Blueprint $table)
{
$table->increments('id');
// this needs to also be nullable, how should the next migration be?
$table->integer('user_id')->unsigned();
}
Run Code Online (Sandbox Code Playgroud) 为什么我会得到错误"类型'字符串'必须是不可为空的值类型才能在泛型类型或方法'System.Nullable'中将它用作参数'T'?"
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using Universe;
namespace Universe
{
public class clsdictionary
{
private string? m_Word = "";
private string? m_Meaning = "";
string? Word {
get { return m_Word; }
set { m_Word = value; }
}
string? Meaning {
get { return m_Meaning; }
set { m_Meaning = value; }
}
}
}
Run Code Online (Sandbox Code Playgroud) 如何使用Postgres完成此操作?我已经尝试了下面的代码,但它不起作用:
ALTER TABLE mytable ALTER COLUMN mycolumn BIGINT NULL;
Run Code Online (Sandbox Code Playgroud) 为了检查a Type(propertyType)是否可以为空,我正在使用:
bool isNullable = "Nullable`1".Equals(propertyType.Name)
Run Code Online (Sandbox Code Playgroud)
有没有办法避免使用魔法字符串?
如果我有一个可空类型Xyz?,我想引用它或将其转换为非可空类型Xyz.在Kotlin这样做的惯用方法是什么?
例如,此代码出错:
val something: Xyz? = createPossiblyNullXyz()
something.foo() // Error: "Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Xyz?"
Run Code Online (Sandbox Code Playgroud)
但是,如果我首先检查null是允许的,为什么?
val something: Xyz? = createPossiblyNullXyz()
if (something != null) {
something.foo()
}
Run Code Online (Sandbox Code Playgroud)
假设我确定它确实从未如此,如何null在不需要if检查的情况下更改或处理值null?例如,在这里,我从地图中检索一个我可以保证存在的值而结果get()不是null.但是我有一个错误:
val map = mapOf("a" to 65,"b" to 66,"c" to 67)
val something = map.get("a")
something.toLong() // Error: "Only safe (?.) or non-null asserted (!!.) calls …Run Code Online (Sandbox Code Playgroud) 有人可以解释为什么这在C#.NET 2.0中有效:
Nullable<DateTime> foo;
if (true)
foo = null;
else
foo = new DateTime(0);
Run Code Online (Sandbox Code Playgroud)
......但这不是:
Nullable<DateTime> foo;
foo = true ? null : new DateTime(0);
Run Code Online (Sandbox Code Playgroud)
后一种形式给我一个编译错误"无法确定条件表达式的类型,因为'<null>'和'System.DateTime'之间没有隐式转换."
并不是说我不能使用前者,但第二种风格与我的其余代码更加一致.
PHP 7引入了返回类型声明.这意味着我现在可以指示返回值是某个类,接口,数组,可调用或新的可阻塞标量类型之一,这对于函数参数是可能的.
function returnHello(): string {
return 'hello';
}
Run Code Online (Sandbox Code Playgroud)
通常情况下,值并不总是存在,并且您可能返回某种类型的某些内容,或者返回null.虽然您可以通过将其默认值设置为null(DateTime $time = null)来使参数可为空,但似乎没有办法为返回类型执行此操作.确实如此,或者我不知道怎么做?这些不起作用:
function returnHello(): string? {
return 'hello';
}
function returnHello(): string|null {
return 'hello';
}
Run Code Online (Sandbox Code Playgroud) 使用Xcode 6.3,引入了新的注释,以便更好地表达Objective-C中API的意图(并确保更好的Swift支持).那些注释当然是nonnull,nullable和null_unspecified.
但是使用Xcode 7,会出现很多警告,例如:
指针缺少可为空类型说明符(_Nonnull,_Nullable或_Null_unspecified).
除此之外,Apple使用另一种类型的可为空性说明符,标记其C代码(源代码):
CFArrayRef __nonnull CFArrayCreate(?CFAllocatorRef __nullable allocator, const void * __nonnull * __nullable values, CFIndex numValues, const CFArrayCallBacks * __nullable callBacks);
总而言之,我们现在拥有这3种不同的可空性注释:
nonnull,nullable,null_unspecified_Nonnull,_Nullable,_Null_unspecified__nonnull,__nullable,__null_unspecified即使我知道为什么以及在哪里使用哪个注释,我也会对我应该使用哪种类型的注释,在哪里以及为什么感到困惑.这是我可以收集的:
nonnull,nullable,null_unspecified.nonnull,nullable,null_unspecified.__nonnull,__nullable,__null_unspecified._Nonnull,_Nullable,_Null_unspecified. …为什么这不编译?
int? number = true ? 5 : null;
Run Code Online (Sandbox Code Playgroud)
无法确定条件表达式的类型,因为'int'和<null>之间没有隐式转换
nullable ×10
c# ×5
generics ×2
eloquent ×1
enums ×1
kotlin ×1
laravel ×1
laravel-5 ×1
non-nullable ×1
null ×1
objective-c ×1
php ×1
php-7 ×1
postgresql ×1
return-type ×1
sql ×1
type-hinting ×1