如何在静态类型语言中复制?

huangapple go评论73阅读模式
英文:

How to replicate in statically typed language?

问题

你不需要在Python中声明返回类型,但在其他诸如Java之类的语言中,你必须事先指定。那么我们如何在静态类型的语言中编写下面的程序呢?返回类型取决于条件是否为真。

public static Object f(int a) {
    if (a > 11) {
        return 200;
    } else {
        return "not valid";
    }
}

如果条件为真,则返回类型为int。如果不是,则为字符串。

英文:

You don't have to declare return type in Python but in other languages like Java you have to specify beforehand. So how can we write below program in a statically typed language? Return type depends on whether condition is true or not.

def f(a):
    if a > 11:
        return 200
    else:
        return "not valid"

If condition is true, return type is int. If not it is string.

答案1

得分: 2

在支持异常的Java或其他编程语言中,你会抛出一个异常。这就是异常的作用:

int f (int a) {
    if (a > 11) return 200;
    throw new Exception();
}

在不支持异常的语言中(例如C),你会返回一个标志值:一个绝对不可能是真实响应的值:

int f (int a) {
    if (a > 11) return 200;
    return -1; // 或者0,或任何其他无效数字
}

一些语言(如Rust、Erlang)允许你返回元组。你可以将元组的第一个元素作为状态(错误/成功),第二个元素作为成功情况下的实际返回值。

英文:

In Java or any other language that supports exceptions, you would raise an exception. That is what exceptions are for:

int f (int a) {
    if (a > 11) return 200;
    throw new Exception();
}

In a language that does not support exceptions (e.g., C), you would return a sentinel: a value that cannot possibly be a real response:

int f (int a) {
    if (a > 11) return 200;
    return -1; // Or 0, or any other invalid number
}

Some languages (Rust, Erlang) allow you to return tuples. You can use the first element of a tuple as the status (error/success) and the second as the actual return value in case of success.

答案2

得分: 1

在静态类型语言中,比如Java,通常有一个超级类型,所有其他类型都属于它。在Java中,这个超级类型是 Object

因此,您可以直接将您的代码转换为以下形式:

public static Object f(int a) {
    if (a > 11)
        return 200;
    else
        return "not valid";
}

然而,这被认为是不好的编程实践。您应该在这种情况下抛出异常:

public static Object f(int a) throws IllegalArgumentException {
    if (a > 11)
        return 200;
    else
        throw new IllegalArgumentException("not valid");
}

在Java中,异常本质上是可以在正常操作之外被抛出的“有效”输出。如果在调用方法中没有被“捕获”,它们将被程序执行器抛出,并且“优雅地崩溃”应用程序。

英文:

In statically typed languages, like Java, there is usually a super type to which all other types belong. In java it is Object.

So you can directly convert your code to the following:

public static Object f(int a) {
    if (a > 11)
        return 200;
    else
        return "not valid";
}

However, this is considered bad programming. You should instead throw an exception in this case:

public static Object f(int a) throws IllegalArgumentException {
    if (a > 11)
        return 200;
    else
        throw new IllegalArgumentException("not valid");
}

In Java, exceptions are essentially "valid" outputs that can be thrown outside of normal operation. If not "caught" by the calling method, they will be thrown by the program executioner, and "elegantly crash" the application.

huangapple
  • 本文由 发表于 2020年7月26日 03:23:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63092599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定