Crossing boundaries: A brief talk on the type strategy outside the Java model

Source: Internet
Author: User

When it comes to the Java language's type approach, the Java community is divided into two broad camps. Some people like compile-time error checking, better security, and improved tools-these are features that static types can provide. Others, however, prefer a more dynamic type experience. This time across boundaries, you'll see some of the very different types of strategies used by two highly productive non-Java languages, and find ways to improve the type flexibility in Java programming.

One of the most controversial issues in the discussion of any programming language is the type model. The type determines which kinds of tools can be used and affects the design of the application. Many developers associate types with productivity or maintainability (I'm one of them). Typical Java developers are particularly happy to maintain the status of the Java language type model, emphasizing that the Java language can take advantage of better development tools, capturing certain kinds of bugs at compile time (such as incompatible type and spelling errors), and performance.

If you want to understand a new programming language, or even a range of languages, you should usually start with type policy. In this article, you will see the type model in some languages outside of Java. Let me first briefly introduce some decisions that any language designer must consider in the type model, focusing on the different decisions of static and dynamic types. I'll show you some different extreme examples of static types in--objective CAML and dynamic types in Ruby. I'll also talk about the type limitations of the Java language and how to break the Java type restrictions for fast programming.

Type policy

You can at least look at the types from three angles:

A static type or a dynamic type depends on when the type model is enforced. The static type language enforces the type at compile time. Dynamic type languages typically implement types at run time based on the characteristics of an object.

Strong type or weak type depends on how the type model is enforced. Strongly typed types are strictly enforced, and if a violation of a type rule is found, a run-time or compile-time error is thrown. And the weak type leaves more room for it. In extreme cases, weakly typed languages (such as assembler) allow arbitrary data types to be assigned to another type, regardless of whether the assignment is meaningful or not. Statically typed languages can have either strong or weak types, whereas dynamic type systems are typically strongly typed, but not entirely.

An explicit type or an implicit type depends on how the language determines the type of a given object. An explicit type language requires declaring each variable and each function parameter. Implicit type language determines the type of object according to the syntax and structure clues in the language. Static type languages are usually explicitly typed, but not entirely, and dynamic-type languages are almost always implicitly typed.

The following two examples illustrate the implications of two of these angles well. Let's say that you compile this section of Java code:

class Test {
public static void test(int i) {
String s = i;
}
}

You receive the following error message:

Test.java:3: incompatible types
found : int
required: java.lang.String
String s = i;
^
1 error

Execute the following Ruby code:

1 + "Hello"

You receive the following error message:

TypeError: String can't be coerced into Fixnum
from (irb):3:in '+'
from (irb):3

Both languages tend to be strongly typed, because when you try to use an object of a type structure that they expect, they throw an error message. The Java type policy gives an error message at compile time because it performs a static type check. Ruby, however, gives the error message at run time because Ruby supports dynamic types. In other words, Java binds an object to a type at compile time. Ruby, however, binds the object to a type whenever the object is changed at run time. Since I declare variables in Java code rather than in Ruby, you can see that the Java language's explicit types work differently from Ruby's implicit type.

In these three angles, static and dynamic types have the greatest impact on the characteristics of the language, so I'll focus on the merits of the two strategies.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.