Type inference

Type inference refers to the automatic detection of the data type of an expression in a programming language.

 

Type inference is the ability to automatically deduce, either partially or fully, the type of an expression at compile time. The compiler is often able to infer the type of a variable or the type signature of a function, without explicit type annotations having been given. In many cases, it is possible to omit type annotations from a program completely if the type inference system is robust enough, or the program or language is simple enough.

To obtain the information required to infer the type of an expression, the compiler either gathers this information as an aggregate and subsequent reduction of the type annotations given for its subexpressions, or through an implicit understanding of the type of various atomic values (e.g. true : Bool; 42 : Integer; 3.14159 : Real; etc.). I

转载于:https://www.cnblogs.com/feng9exe/p/9151219.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Flink 1.15.0 版本中,`TypeInferenceUtil.createRowTypeInfo` 方法已经被移除了,取而代之的是 `TypeConversions.fromLegacyInfoToDataType` 方法来创建 `RowType`。 如果你需要在 `TableFunction` 中设置多个输出参数的类型,可以使用 `TypeInference.newBuilder()` 方法来创建一个 `TypeInference` 对象,并使用 `output` 方法为输出参数添加类型信息。下面是一个示例: ```java import org.apache.flink.api.common.typeinfo.TypeInformation; import org.apache.flink.streaming.api.functions.table.TableFunction; import org.apache.flink.table.api.DataTypes; import org.apache.flink.table.api.TableException; import org.apache.flink.table.functions.FunctionContext; import org.apache.flink.table.functions.TableFunction; import org.apache.flink.table.types.DataType; import org.apache.flink.table.types.inference.TypeInference; import org.apache.flink.table.types.inference.TypeInferenceUtil; import org.apache.flink.table.types.logical.RowType; import org.apache.flink.table.types.utils.TypeConversions; public class MyTableFunction extends TableFunction<Row> { private final String separator; public MyTableFunction(String separator) { this.separator = separator; } @Override public void open(FunctionContext context) throws Exception { super.open(context); } public void eval(String str) { String[] fields = str.split(separator); collect(Row.of(fields[0], fields[1])); } @Override public TypeInference getTypeInference(DataTypeFactory typeFactory) { return TypeInference.newBuilder() .outputRows(DataTypes.ROW( DataTypes.FIELD("field1", DataTypes.STRING()), DataTypes.FIELD("field2", DataTypes.STRING()) )) .build(); } } ``` 在上面的示例中,我们首先使用 `TypeInference.newBuilder()` 方法创建一个 `TypeInference` 对象,并使用 `outputRows` 方法为输出参数添加类型信息。`DataTypes.ROW` 方法可以用于创建一个 `RowType` 对象,指定每个字段的名称和类型。 最后,我们将 `TypeInference` 对象返回给 `getTypeInference` 方法即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值