官术网_书友最值得收藏!

Limiting the scope of failed assumptions

As you know, the use of var types is limited to local variables in Java. They are not allowed in the public API, as method parameters or as the return type of methods. Some languages support type inference for all types of variables. Java may allow us to do so in the future, but we don't know when.

However, there are strong reasons for limiting the scope of the inferred variables, to spot the errors due to mismatch of assumptions and the actual, early on. The contracts of the public APIs should be explicit. Type inference with public APIs would allow for these errors to be caught and corrected much later.

The contract of the public APIs should be explicit; they shouldn't depend on type inference.

The following is a practical example of how a mismatch between an assumption and the actual case can lead to errors.

Recently, my daughter was traveling overseas with her school for a student exchange program. The school asked me to send back a set of photographs for her visa application. I called a photographer, requesting that he print photos for the visa (and specifying the country). Two days later, the school asked me to resubmit the photos because they didn't match the rules.

What went wrong? Neither the school nor myself were explicit with the specifications of the photograph. The school assumed that I would know the specifications; I assumed that the photographer would know the specifications (because he had been doing it for years). In this case, at least one person assumed that the result conformed to a specific output, without explicitly specifying the output. Without an explicit contract, there is always the chance of mismatching the expectation with the actual case.

Despite the confusion, the mistake was spotted and corrected before the applications were submitted to the embassy.

The following is a fun image, included showing why the use of type inference is limited to local variables. The local instances and static variables are competing in a race, and only the local variables can make it to the finish line:

主站蜘蛛池模板: 新竹县| 获嘉县| 东丽区| 松潘县| 黔南| 汽车| 宜兰县| 盐源县| 宿松县| 察隅县| 盐源县| 泰顺县| 霍州市| 石棉县| 禹城市| 阳山县| 永新县| 行唐县| 长沙县| 永济市| 凤翔县| 阜新市| 丹江口市| 新乐市| 阆中市| 广德县| 东平县| 萨迦县| 芮城县| 隆化县| 甘南县| 西峡县| 息烽县| 黄石市| 达孜县| 平罗县| 大同市| 沧州市| 施甸县| 南京市| 乌兰察布市|