火和忘记方法(Fire and Forget approach)
与此答案相关的是,
如果我确实想要“消除并忘记”一种确实会返回任务的方法,并且(为了简单起见),让我们假设该方法不会抛出任何异常。 我可以使用答案中列出的扩展方法:
public static void Forget(this Task task) { }
使用这种方法,如果
Task
存在导致抛出异常的错误,那么当抛出异常异常时,异常将被吞下并且不被注意。问题:在这种情况下,扩展方法的形式不是更合适吗?
public static async void Forget(this Task task) { await task; }
所以编程错误会引发异常并升级(通常会导致进程失败)。
对于具有预期(和可忽略)异常的方法而言,该方法需要变得更加复杂(作为一个旁白,关于如何构建此方法的一个版本的任何建议,这个版本将采用可接受和可忽略的异常类型的列表? )
Related to this answer,
If I truly do want to "Fire and Forget" a method that does return a task, and (for simplicity) let's assume that the method isn't expected to throw any exceptions. I can use the extension method listed in the answer:
public static void Forget(this Task task) { }
Using this approach, if there are bugs in action of the
Task
that cause an exception to be thrown then when the unexpected exception is thrown, the exception will be swallowed and go unnoticed.Question: Wouldn't it be more appropriate in this scenario for the extension method to be of the form:
public static async void Forget(this Task task) { await task; }
So that programming errors throw an exception and get escalated (usually bringing down the process).
In the case of a method with expected (and ignorable) exceptions, the method would need to become more elaborate (as an aside, any suggestions on how to construct a version of this method that would take a list of acceptable and ignorable exception types?)
原文:https://stackoverflow.com/questions/22864367
最满意答案
(setq f (open "c:\\eng000.txt" "r")) (princ (read-line f))
其他回答
如何查看 common lisp 的标准函数(这么叫不太准确,规范的名称似乎是操作符 symbol ) 对于初学者来说,学习使用 lisp 有一点很不习惯的地方就是不知道怎么查帮助,比如想了解下 lisp 有哪些自带的函数(c-c c-d h 调用了 slime-hyperspec-look...
相关问答
更多-
请问如何正确使用commonlisp的read-line函数?[2024-04-01]
(setq f (open "c:\\eng000.txt" "r")) (princ (read-line f)) -
read-line是正确的功能.. (println (read-line)) ..基本上会回应用户的输入: Clojure 1.0.0- user=> (println (read-line)) this is my input this is my input 要在if语句中使用它,你可能会使用let: (let [yayinput (read-line)] (if (= yayinput "1234") (println "Correct") (println "Wrong") ...
-
我想你可能想要做的是.. import re import time import serial def doRead(ser,term): matcher = re.compile(term) #gives you the ability to search for anything tic = time.time() buff = ser.read(128) # you can use if not ('\n' in buff) too if you ...
-
assoc返回包含新映射的新映射。 您没有将这些新地图分配给任何内容,因此它们将被丢弃。 您需要跟踪中间映射 - 从输入序列重复更新状态的一种方法是使用reduce : (defn prompt [m [msg key]] (println msg) (assoc m key (read-line))) (defn getVals [] (reduce prompt pc [["Enter Name: ", :Name], ["Enter Class: ", :Race], ["Ente ...
-
我不认为它打印两次。 我认为它是打印 (通过写入行 )一次,然后写入行 返回该行,并且REPL正在打印返回值。 例如,如果你修改你的mytest以返回nil ,我想你会看到你期望的输出: (defun mytest (command) (let ((socket (usocket:socket-connect _IP_ _PORT_))) (socket-print command socket) (socket-read socket) nil)) ...
-
我建议重写该过程,它似乎不是读取文件并循环其行的正确方法。 请试试这个: (define (f) (let loop ((line (read-line))) (if (not (eof-object? line)) (begin (format #t "line: ~a\n" line) (let ((m (string-match "^[ \t]*#" line))) (if m (format #t "comm ...
-
将严重的骆驼案件重命名为lisp案例我就离开了。 (defun next-states (st) "generate all possible states" (loop :for action :in (possible-actions) :for aux := (next-state st action) :then (next-state st action) :when (not (member aux states-list :test #'equalp)) ...
-
如果你将swank.core / with-read-line-support中的read-line调用包装起来,那么现在可以使用swank-clojure 1.4.0-SNAPSHOT (with-read-line-support (println "a line from Emacs:" (read-line)) https://github.com/technomancy/swank-clojure/commit/f4a1eebc4d34f2ff473c4e5350f889ec356f5168 T ...
-
依次返回符号`with-redefs`(Clojure)的不同值,(Return successively different values for a symbol `with-redefs` (Clojure))[2023-12-28]
不是特别的,但你总是可以在某种状态下“放过lambda”! (let [a (atom ["a" "b"])] (defn f [] (let [r (first @a)] (swap! a rest) r))) (f) ;; "a" (f) ;; "b" (f) ;; nil 在你的特定情况下,有一个产生'有状态'功能的函数是有意义的,所以一个完整的例子是: (defn maker [l] (let [a (atom l)] (fn [] ( ... -
show procedure没有定义。 作为鸡计划中实施的R5RS的一部分,您可以使用display或write输出,如文档中所示。 但是, show的功能很容易实现: (define (show obj) (display obj) (newline)) The show procedure is not defined. As part of the R5RS implemented in Chicken Scheme, you can use display or write for out ...