小编kha*_*hik的帖子

如何在F#中使用有意义的消息抛出异常?

如何在F#中抛出有意义的异常并在C#中捕获它们?

使用以下代码:

F#图书馆:

module Test

exception TestExc of string

let throwit message : unit=
    raise (TestExc("custom exception with message: " + message))
Run Code Online (Sandbox Code Playgroud)

C#客户端:

namespace TestExc
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Test.throwit("Hi there");
            }
            catch (Test.TestExc te)
            {
                Console.WriteLine("Caught exception with message: " + te.Message);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到以下消息:

Caught exception with message: Exception of type 'Test+TestExc' was thrown.
Run Code Online (Sandbox Code Playgroud)

但我希望看到Hi there被捕获的异常消息.

以下F#等价物是什么?

class CSTestExc:System.Exception 
{
        public CSTestExc(String message) : base(message) { } …
Run Code Online (Sandbox Code Playgroud)

c# f# exception

2
推荐指数
1
解决办法
781
查看次数

?:三元运算符中的Map.get()优化

请考虑以下代码:

java.util.Map<String, String> map = new java.util.HashMap<String, String>();
...
String key = "A";
String value = map.get(key) == null? "DEFAULT_VALUE" : map.get(key); // (1)
Run Code Online (Sandbox Code Playgroud)

编译器优化生产线(1)类似的东西:

String tmp = map.get(key);
String value = tmp == null? "DEFAULT_VALUE" : tmp;
Run Code Online (Sandbox Code Playgroud)

(或者:

String value = map.get(key);
if(value == null) value = "DEFAULT_VALUE";
Run Code Online (Sandbox Code Playgroud)

)?

java optimization map compiler-optimization

2
推荐指数
1
解决办法
1585
查看次数

UNIX中的重定向运算符

假设我有三个文件file1 file2 file3有一些内容现在当我在shell提示符下执行此操作时 cat file1 > file2 >file3

file1的内容被复制到file3,file2变为空

类似的,当我这样做时cat > file1 > file2 > file3 它要求输入,这个输入存储在file3中,file1和file2都是空的

并且cat > file1 > file2 < file3file3的内容也被复制到file2,file1为空.

有人可以向我解释发生了什么我是UNIX的新手.还有任何我可以了解这些重定向运算符的网站.

谢谢

unix shell redirect

1
推荐指数
1
解决办法
1661
查看次数

使用`is`检查空字符串

is在Python中检查空字符串是否正确?它进行身份检查,同时==测试相等性.

考虑以下(使用的想法join是从这个答案借来的):

>>> ne1 = "aaa"
>>> ne2 = "".join('a' for _ in range(3))
>>> ne1 == ne2
True
>>> ne1 is ne2
False
>>>
Run Code Online (Sandbox Code Playgroud)

所以这里的is工作可以预期.现在来看看这段代码:

>>> e1 = ""
>>> e2 = "aaa".replace("a", "")
>>> e3 = "" * 2
>>> e4 = "bbb".join(range(0))
>>> e1, e2, e3, e4
('', '', '', '')
>>> e1 is e2
True
>>> e1 is e3
True
>>> e1 is e4
True
>>> id(e1), …
Run Code Online (Sandbox Code Playgroud)

python identity equals

1
推荐指数
2
解决办法
1399
查看次数

char*的printf获取Segmentation Fault

我正在尝试从套接字读取并使用printf打印到stdout(必须);

但是,每当我从理智的网站上读取特定文件(HTML)时,我都会收到分段错误.

请看一下这段代码并告诉我有什么不对.

int total_read = 0;
 char* read_buff = malloc(BUF_SIZE);
 char* response_data = NULL;
 if (read_buff == NULL){
  perror("malloc");
  exit(1);
 }
 while((nbytes = read(fd, read_buff, BUF_SIZE)) > 0){
  int former_total = total_read;
  total_read += nbytes;
  response_data = realloc(response_data, total_read);
  memmove(response_data + former_total, read_buff, nbytes); //start writing at the end of spot before the increase.
 }
 if (nbytes < 0){
  perror("read");
  exit(1);
 }

 printf(response_data);
Run Code Online (Sandbox Code Playgroud)

谢谢.

c printf segmentation-fault

0
推荐指数
1
解决办法
2222
查看次数

python多重继承:避免以菱形形状调用构造函数两次

请考虑以下代码:

class A(object):
    def __init__(self):
        print("A.__init__")
        super(A, self).__init__()          # 1
        print("A.__init__ finished")

class B(A):
    def __init__(self):
        print("B.__init__")
        super(B, self).__init__()          # 2
        print("B.__init__ finished")

class C(A):
    def __init__(self):
        print("C.__init__")
        super(C, self).__init__()
        print("C.__init__ finished")

class D(B, C):
    def __init__(self):
        print("D.__init__")
        print("Initializing B")
        B.__init__(self)                   # 3
        print("B initialized")
        print("Initializing C")
        C.__init__(self)                   # 4
        print("C initialized")
        print("D.__init__ finished")

D()

# D.__init__
# Initializing B
# B.__init__
# C.__init__
# A.__init__
# A.__init__ finished
# C.__init__ finished
# B.__init__ finished
# B initialized
# Initializing …
Run Code Online (Sandbox Code Playgroud)

python initialization multiple-inheritance super diamond-problem

0
推荐指数
1
解决办法
1572
查看次数