Thrift is not strictly a programming language, but it beats a lot of programming languages and is aesthetically pleasing.

Underlying data types

The Thrift programming language provides the following basic data types:

  • bool: A boolean value (true or false)
  • byte: An 8-bit signed integer
  • i16: A 16-bit signed integer
  • i32: A 32-bit signed integer
  • i64: A 64-bit signed integer
  • double: A 64-bit floating point number
  • string: A text string encoded using UTF-8 encoding

In general, we use just those, just like any other everyday programming language. For example, I am basically this “three plate axe” :

  • bool
  • I32 (now gradually used i64, because the performance of what, I am not the first concern)
  • double
  • string

Complex data types (containers)

Let’s take a look at what container types Thrift provides:

  • list: An ordered list of elements. Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
  • set: An unordered set of unique elements. Translates to an STL set, Java HashSet, set in Python, etc. Note: PHP does not support sets, so it is treated similar to a List
  • map: A map of strictly unique keys to values. Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc. While defaults are provided, the type mappings are not explicitly fixed. Custom code generator directives have been added to allow substitution of custom types in various destination languages

It’s almost the same as C++ STL, with the same names. The list data structure is actually a “dynamic array”, and the name alone makes it easy to refer to a linked list. This phenomenon is also found in other programming languages, such as Python, which is also called a list.

The class, that is, struct

A slightly more normal language with OOP support is essential, and I think it’s best to provide the class keyword directly, with as much semantic clarity as possible.

But Thrift only has one struct, which is basically the same as C struct, and it has very little functionality, but that’s understandable given that it’s just an intermediate language.

A well-written struct should be defined as clear and complete:

struct Person { 1: required string name; 2: required i64 age; 3: optional string addr; // Optional Field 4: Optional string defaultValue ="DEFAULT"; // Default field 5: string otherValue; // Not so clear! }Copy the code

Interface, that is, the service

Under the principle of “interface oriented programming”, “interface” is an important factor. Some people call them functions, some call them methods, and in this article we call them collectively “methods.”

In Thrift, defining an interface is a simple matter (an example from the Thrift website) :

// Interface, can also inherit, maybe we can sometimes make a "BaseService" or something like that, but I rarely use it. Service Calculator extends shared.SharedService {// the normal method is basically the same as in a traditional language like C++. void ping(), i32 add(1:i32 num1, 2:i32 num2), i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch), {oneway void zip()} oneway void zip() {oneway void zip()}Copy the code

Exception

The thing about an exception in Thrift is just like defining a struct, because an exception is conceptually a class, and everything is an object. But now we use the “exception” keyword, which also fits the clear semantics I mentioned earlier. Let’s look at how the exception in Thrift is defined:

exception InvalidOperation {
  1: i32 whatOp,
  2: string why
}
Copy the code

The enumeration

Enumerating this thing is so important that, like the exception above, it’s just a class. In Thrift, we only support enumerations of ints. Unfortunately, in many cases, we support enumerations of strings. The enumeration in Thrift is as follows:

ADD = 1, SUBTRACT = 2, MULTIPLY = 3, DIVIDE = 4}Copy the code

If you like my article, please follow my official account: “Programming like a Dream”. Also check out my Jane’s book column, “Programming in a Dream.” Or join my planet of knowledge, “Programming like a dream”, for more dry stuff.