例如,Exception.allCatch定义为
def allCatch[T]: Catch[T]
Run Code Online (Sandbox Code Playgroud)
为什么不呢
val allCatch: Catch[Nothing]
Run Code Online (Sandbox Code Playgroud)
什么时候Catch它的论证是协变的?
或者,为什么PartialFunction对象定义
def empty[A, B]: PartialFunction[A, B]
Run Code Online (Sandbox Code Playgroud)
而不仅仅是
val empty: PartialFunction[Any,Nothing]
Run Code Online (Sandbox Code Playgroud)
?
更新:到目前为止似乎答案错过了重点.因此,请在答案中包含一个真正针对问题的具体示例.例如:显示一段可以使用def empty[A, B]: PartialFunction[A, B]但不起作用(或不太方便)的代码val empty: PartialFunction[Any,Nothing].
我在使用结构和函数时遇到了一些麻烦,这些函数在VB.NET中返回Nothing.
让我试着用这段代码解释一下:
Public Class Form1
Structure Test
Dim field1 As String
End Structure
Private Function Foo() As Test
Return Nothing
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim st As Test = Foo()
End Sub
End Class
Run Code Online (Sandbox Code Playgroud)
在前面的代码中,当我返回Nothing作为Foo函数的结果时,我希望st是Nothing.但这不是发生的事情.
然后我在MSDN文档中找到:
为变量赋值Nothing将其设置为其声明类型的默认值.如果该类型包含变量成员,则它们都设置为其默认值.
所以我发现当我为结构分配Nothing时,它的所有成员都被设置为默认值,而不是结构本身.
另外,我试图通过声明:使st成为Nullable类型:
Dim st As Nullable(Of Test) = Foo()
Run Code Online (Sandbox Code Playgroud)
但是,我仍然无法使用以下方法检查st是否为Nothing:
If st Is Nothing Then
Run Code Online (Sandbox Code Playgroud)
要么
If st.Equals(Nothing) Then
Run Code Online (Sandbox Code Playgroud)
那么,问题:
1 - 是否可以为结构而不是其成员分配 …
在我自己的代码和许多邮件列表发布中,我注意到混乱,因为没有任何东西被推断为两个其他类型的最小上限.
答案可能对你来说很明显*,但我很懒,所以我问你*:
在什么条件下推断这样的结果是最理想的结果?
让编译器在这些情况下抛出错误或警告除非被某种注释覆盖是否有意义?
*多个
我已经开始尝试Haskell并遇到问题.qqq是一个函数,如果使用"Nothing"调用则应打印一个字符串,如果使用"Just something"调用则打印其他内容.
第一次尝试似乎工作:
qqq Nothing = print "There isn't anything to be printed."
qqq (Just x) = print "There is something to be printed." >> print x
main :: IO ()
main = qqq (Just 43)
Run Code Online (Sandbox Code Playgroud)
但:
main = qqq (Nothing)让它失败时(约束中的"模糊类型变量'a0':(显示a0)因使用'qqq'而产生"")qqq :: Maybe x => x -> IO ()- > Type constructor 'Maybe' used as a class- >但不是吗?qqq :: (Maybe x) -> IO ().现在签名本身看起来很成功.但是,如果遇到main = qqq (Just 43)这种神秘的(Show a0) …直截了当的问题:我有一些双倍的变量.我希望能够在其中存储"null"状态,即我需要能够表示该变量不包含有效数据.我真的不想将一个布尔"有效"变量与每一个双关联,这将是丑陋的,可能是不必要的.
首先,我发现必须以不同的方式声明变量以允许检查'IsNothing'的概念,所以我这样做:
dim someDouble as Double?
Run Code Online (Sandbox Code Playgroud)
(注意问号).如果我没有这样声明,错误检查会给我一个"IsNot需要具有引用类型的操作数"消息.
声明后,将变量设置为......
someDouble = Nothing
Run Code Online (Sandbox Code Playgroud)
...似乎将它设置为零,因为它永远不会运行我的if/else语句中的代码来检查是否someDouble IsNot Nothing...这是不好的,因为该变量可以合法地将0存储为有效的数据.
我在这里错过了什么?谢谢!
编辑:我遗漏了我正在使用类中的属性Get和Set这些值.事实证明我正在做正确的事情,除了我把我Property的类型留作一个Double而不是一个Double?所以它回到零而不是Nothing值.但有用的信息仍然在下面的答案中!
为什么会有所不同?!
Public Class Form1
Public Function MyFunction() As Integer?
Return Nothing
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim o As Object = Me
MsgBox(TypeName(Me)) ' Form1
MsgBox(TypeName(o)) ' Form1
MsgBox(TypeName(Me.MyFunction())) ' Nothing
MsgBox(TypeName(o.MyFunction())) ' Nothing
' but
MsgBox(TypeName(Me.MyFunction() + 0)) ' Nothing
MsgBox(TypeName(o.MyFunction() + 0)) ' Integer
End Sub
End Class
Run Code Online (Sandbox Code Playgroud) F#的Condtional表达式需要检查条件,分支为true,分支为false.例如:
let x =
if ("hello" = null)
then true
else false //error if else branch missing
Run Code Online (Sandbox Code Playgroud)
然而,当事情会很奇怪unit,又名(),参与.
let y =
if ("hello" = null)
then raise <| new ArgumentNullException()
else () //happy with or without else branch
Run Code Online (Sandbox Code Playgroud)
而且更简单:
let z =
if ("hello" = null)
then ()
else () //happy with or without else branch
Run Code Online (Sandbox Code Playgroud)
返回else时为什么不需要分支unit?
我对 Julia 很陌生,但我对 Scheme/Rust/F# 有一定的了解。
今天我想让昨天的 AoC更好,而不需要明确数量的嵌套循环。
我找到了这个可行的解决方案,但我不喜欢最后一个if. 在上面提到的语言中,我会调用一个函数(或使用计算表达式),它给我的第一个结果不是None. 对于朱莉娅,我期望的东西做的。它确实如此,但出乎意料地以一种热切的方式出现。
所以当我尝试时return something(find(r, n, start + 1, which), find(r, n - 1, start + 1, extended)),当第一个参数已经有结果时,它也会评估第二个参数 - 因此崩溃。
是否有宏/懒惰版本或something我没有找到?遇到这样的案子该怎么办?
我也想过(短路)或将它们放在一起,但我猜朱莉娅在这件事上的严格破坏了这一点。
using DataStructures
function find(r::Array{Int}, n, start = 1, which = nil())::Union{Int,Nothing}
if start <= length(r)
extended = cons(start, which)
with_current = sum(i -> r[i], extended)
if with_current == 2020 && n == 1
return prod(i -> r[i], …Run Code Online (Sandbox Code Playgroud) 我在VB.net中有一个声明,我认为我写的正确,以防止后半部分被评估.它看起来像这样:
If((myDataSet2 IsNot Nothing)或myDataSet2.Tables("CurData").Rows.Count> 0)
但是它不会跳过第二个表达式"myDataSet2.Tables("CurData").Rows.Count> 0"就像我想要的那样.
我应该改变什么?
下面的代码Object Required (Error 424)在 if 语句中抛出与Nothing给定值无关的比较。为什么?
Public Function SqlizeCellValue(ByVal Value As Variant) As Variant
If Value Is Nothing Then
SqlizeCellValue = Nothing
ElseIf Value = Null Then
SqlizeCellValue = Null
ElseIf Value = "" Then
SqlizeCellValue = Null
ElseIf Value = "0" Then
SqlizeCellValue = Null
ElseIf Value = "---" Then
SqlizeCellValue = Null
ElseIf LCase(Value) = "n.c." Then
SqlizeCellValue = Null
Else
SqlizeCellValue = Value
End If
End Function
Public Sub TestSqlizeCellValue()
Debug.Assert SqlizeCellValue("") Is …Run Code Online (Sandbox Code Playgroud)