The Curse of Braces

Let's say you are a large company. Engaged in the development of the browser, mailer and even the OS for smartphones. Suddenly, you realize that you don't like the modern and powerful C ++ language. Well, or like it, but it's impossible to work with him. It happens. And here you are, an adult stable company decides to develop a new programming language. The recipe is simple. Take a barrel of money, books on the history of IT, a company of programmers and a MacBukov truck. Think long, quickly code, breed hype in social networks (reddit, facebook, github). And so, in 2015, you give the community The Language. Let's call him Yaist.

Language is a tool for professionals. Works by profession means professional. What does a professional read first when learning a new language? Of course, an objective opinioncolleagues. It is mostly positive. Following the opinion of colleagues, a professional reads a description of the language. More than two hundred pages. Oh. There will be something to kill time on the subway.

Fast start


Hello World!


The most important stage of study.

fn main() {
  println!("Hello, world!");
}

What do we see? fn , curly braces, semicolon, println! . This is a macro, as the author explains to us. A string constant is a string constant.

Obviously, the developers worked on a shorthand for the fn keyword. This is not a screaming PROCEDURE that you need to write for a long time, you still need to hold Shift. This item is a definite improvement over the eighties. The main () function returns nothing. Emptiness. The result is void, you do not even need to specify it. Try explaining void to fifth graders. That's the same.
Curly operator brackets. Such brackets are known to save memory on 70s machines. Now they save programmer time. He has no time to write BEGIN END, because the 21st century is outside the window. Productivity Time.

Macro println! draws attention to an exclamation mark. This is not a function for you. However, we do not know anything about the origin of the macro, since full qualifiers of entities are not needed. Maybe it’s even built into the language, because in the example there is no import section (in fact, there is, any program imports a limited set from the std library). No time to figure out, just println! In the 90s, it would be written as Log.String ("Hello, World!"). It was a dashing time, we survived as best we could.

Do not forget the semicolon. This is an expression based programming language. A semicolon will separate one bracket from another. The procedure ends with a closing bracket. In the 90s, it would have been completed by END Main. It’s good that those days have already passed.

Cargo


Cargo is a package manager. Downloads dependencies, builds them. Yaist suggests the cult of cargo. In the 90s this was not. Here we must pay tribute to the authors. A barrel of money was drunk not in vain. Cargo's programming language is toml - ini on steroids.

Examples


Further, the author of the book considers an example of a guessing game.

use std::io;
fn main() {
  println!("Guess the number!");
  println!("Please input your guess.");
  let mut guess = String::new();
  io::stdin().read_line(&mut guess)
    .ok()
    .expect("Failed to read line");
  println!("You guessed: {}", guess);
}


We must handle user input, so we need to use the io library from the std library. Use std :: io, Luke; And again, we witnessed how the authors of the language carefully worked on the syntax. It used to be like, IMPORT StdIO, you had to write in capital letters. Ruthless attitude to the hands of a programmer. And two colons. Maybe it's like four pluses in the # symbol. Macro println! already seen.

This is followed by a variable declaration, which is combined with initialization and placed directly in the code. Well, how else, do not do the same separate section of variables, as in the eighties. Then the gloomy collective farmers wrote all the variables in a strictly allocated place, so that later they could be found in this place. This is how they restricted the freedom of expression of creative individuals. Forced to order. It’s good that we got rid of this relic.

The presence of mutable and immutable pseudo-variables takes us to a new level of control over data. Variables are no longer variable. The future has come.

String is a string. new () is new (). Then there will be io :: stdin () with the current interface and println! with the ability to automatically substitute arguments in the resulting string.

The development of this example is to generate a random number using a third-party library.

extern crate rand;


So, it turns out, use is not an import, it is a choice from an already imported library. In the past, everyone did this with one team, and only a deep study of the problem showed that the inclusion of everything in a row in the source code leads to a swelling of the resulting binary. Therefore, we need to manage dependencies, we must choose what we will use. Wacky ideas for separate compilation are a thing of the past. We will not regret them.

match guess.cmp(&secret_number) {
  Ordering::Less => println!("Too small!"),
  Ordering::Greater => println!("Too big!"),
  Ordering::Equal => println!("You win!"),
}


The cmp () method is used to compare everything comparable. The result is compared with the predefined values ​​from Enum using the match operator. Previously, for this was the CASE operator, a conditional choice. But now other times. Living in a new way. The arrow => which you could take as a compound comparison operator from the point of view of Arabic writing is actually a pointer to an expression corresponding to a matching variant.

With the loops in the example, all is well. There is a break and continue, all as in adult languages. Dijkstra has already died, you can not worry that he will not approve of a way out of the middle of the cycle. This does not complicate the understanding of the cycle. In the 90s it was complicated, and now people are smarter than they are, and tools are more powerful. And in general, professionals are not mistaken.

Philosophers eat


Philosophers Structures Eat using the eat () method. The author smoothly moves on to the example of philosophers. It introduces the reader to the course that there are structures, they have related functions with the & self parameter (analogous to this). In the 90s, you could call a connected object as you like, in 2015 such a feature is too complicated to implement. We read about the new method, it constructs a new instance of the class. May have custom parameters.

Several philosophers are listed using the vec macro ! . We do not indicate the type of the list variable. But he is.
Primitives of parallel programming are introduced into the program.

let handles: Vec<_> = philosophers.into_iter().map(|p| {
  thread::spawn(move || {
    p.eat();
  })
}).collect();


Saving the efforts of the programmer in everything in naming methods. in closure syntax. In the absence of the need to indicate the data type. But what, and so everything is clear. This is Peh, this is the Century. Programming threads in the example relies on mutexes. Well, good. And then in the 90s all kinds of active objects were conceived. Continuous problems from this. There is nothing better than the good old mutex if you want to write serious adult software. Meanwhile, the syntax for import descriptions is gaining new capabilities.

use std::sync::{Mutex, Arc};


Language features are the cornerstone of success.

Using Yist from the outside


After philosophical dinners, the author decides to focus on more mundane things - on using Yaist to build a shared library.

This is not so interesting from the point of view of the programmer, because until now the functionality of shared libraries in Windows / Linux is pretty poor. There is no way to get information about the interface, problems with cross-platform, problems with versioning (more precisely, the lack of versioning, which becomes a problem of client code), authors of high-level languages ​​have to land at C level. Some languages ​​generally cannot fall so low. But these are their problems. Indeed, the year 2015 is in the yard, and the technology for using the dll is proven, reliable. Professionals have long had tools like FFI. So do not think that all the technology of the seventies is something bad. Whatever they point out, those are bad, and some are good. For example, modularity at the shared library level.

#[no_mangle]
pub extern fn process() 


This is the nickname of Gog Shared Library on Yist.

Effective Yist



Memory management


To work efficiently, first of all we are told about the stack and a bunch. Like, as it is progressive, do not place everything at once on the heap, but lay it on the stack. An entire paragraph is devoted to this memory management model. I remember that in ancient times this was declared an unimportant property. And for the argument in defense of the language was not accepted. But history made a circle and everything again, as before. But they will not tell anyone about this, because the business model does not imply such revelations.

Function Parameters


Then they talk about the arguments of functions, about the fact that passing a pointer to a variable is not transferring the value of a variable. I remember in the past there were even languages ​​in which parameters were divided by access levels inside procedures and this was mainstream.

Testing


Testing is the next step in immersing yourself in effective work with Yist. We are introduced to the assert macro ! and its variations. Programming on assertion does not cause any urge to joke, it is a reliable means of protecting your code from garbage data at the input and unexpected results at the output.

Conditional compilation


Another unexpected guest from the past. More recently, he was criticized for violating expectations from the compilation process. But now, in 2015, we can afford it.

In general, most of the listed features rely on the annotation mechanism, which, it seems to me, is a double-edged tool that, on the one hand, allows you to control the operation of the compiler / linker, and, on the other hand, transferring programming to annotations raises doubts about the correctness of the choice of tool.

Documentation


She is. Even with HTML. We can a lot.

Iterators


A useful abstraction, the author describes how to work with it, describing some design patterns that are used in the implementation of Yaist iterators. Embedding collections in a language once allowed us to get out of control of hundreds of thousands of programmers, hundreds of thousands of implementations of lists and associative arrays. In 2015, to not have support for collections in the language itself is fu and bad manners. Professionals will not approve.

Multithreading


Threads, mutexes, channels, safe transfer between threads, mutability / immutability. Verified-reliable.

Ffi


Welcome to the world of C-function interfaces and C-data types. CMPC, in the world of butterflies and ponies, no one will have to use these terrible tools. In the meantime, you have to live in the world of FFI, where the spirit of the seventies and freedom from the order of the parameters are laid out on the stack.

Borrow and asref traits


Trait is a declaration of a method interface that is used for subsequent implementation in one or more structures. Something between an interface class and an impurity. If you hardly understand why to invent a new word for this, then do not worry. Most likely, the developers apply the principle of greatest surprise to break through the ossified patterns in your brain.

Yist l4ngu4g3 6eZNOGNM



Section 5 describes the language itself with an explanation of the semantics of a particular keyword. The description is quite long, funny moments have already appeared in simple examples. Among the fun facts, there are several:
  • everything is an expression, but we can mark with a special sign the functions that do not return anything, the depth of elaboration of the abstract part is felt
  • the if statement returns the result (yes), replacing the ternary operator
  • built-in transmute statement that leaves a hole in the integrity of data types
  • the unsafe operator is aimed at security

Ownership of objects and lifetime, like features, perhaps simple ones, the ties go to scope, affecting heap (due to the lack of garbage collection), this whole thing is complicated by closures, and as a result, the description of the concept is quite complicated, and they write about it on Habré more than one article in an attempt to understand and forgive to explain.

In general, the syntax and concepts that are hidden behind it (or are not hidden) leave a painful feeling. Complex examples are filled with a scattering of colons, exclamation marks, brackets. Of course, language producers are not very worried about this. It is their business, what kind of semantics to fill with one or another symbol.

Conclusion


Of course, this article is just a fun way to convey my feelings from learning a fresh programming language. Through the prism of my experience, I perceived a particular solution that I met during the study. Of course, I missed some features. But the goal was a bit of it.

The cultural features of the ecosystem of the Company are unknown to me, perhaps this is how the language should look in which formalize their thoughts and ideas of the community of smart people. But the dualism of the situation lies in the fact that Yaist is largely predetermined by the personal characteristics of the creators who have lived all their lives, with the “curse” of languages ​​with curly braces and are now spending their energies on maintaining the ecosystem of the curse in people's heads.

And there may well be a situation in which groups of people formulate some formal languages ​​with an unconscious, unmanifest goal to isolate from the surrounding majority a subgroup of people with a similar attitude. If this strange hypothesis is at least a little close to reality, then I have to admit that I do not belong to this subgroup of people. Perhaps for my case there are more sonorous and vivid definitions that this subgroup will apply to me after reading the article.

I saw in the Yaist language a meaningless following of existing external signs with a consistent substitution of the concepts existing inside the paradigm of imperative programming with new ones invented within the Company. In a few years, the Yaist language will become a part of this reality, and the people who study it and put it into practice will change themselves. And they will begin to think in concepts that were not there yesterday. Those concepts that the Company came up with for them, spending resources on a product that is convenient only for a certain group of people.

In such a creeping way, human communities advance their vision of the world forward, carry out the expansion of their worldview to others, reward the loyal, co-opt the smart and punish dissenters. Such is the course of history.

The word "curse" is allegorical. To convey the meaning of the phenomenon. Do not take to heart. Peace to you.

Also popular now: