How to design a programming language (a) what is a pit (a)

Source: Internet
Author: User
Tags define definition function definition

The cause of this series is this, Wang Yin wrote an article on the Go blog http://www.yinwang.org/blog-cn/2013/04/24/go-language/, which said went is rotten to the hopeless, has not bothered to say, So let everyone go to see http://www.mindomo.com/view.htm?m=8cc4f95228f942f8886106d876d1b041, which has a detailed explanation. Then the article was sent on Weibo, and many bloggers immediately displayed the ugly side of human nature:

1, those who go to the fans, because go was sprayed, feel that their personality was insulted, just too late to see the last link, began to show their claws.

2, Wang Yin This person is indeed with people do not come, so many people to conclude that his things "no reference value."

But to be honest, the article is a bit impolite, it also to some extent to prevent those ignorant people continue to read the essence of the following. If all the articles are so good ah, then rotten people will always be rotten people, do not correct their mentality will never get any useful knowledge, and never have the kind of a monthly income of a maggot day, with garbage language painful write a lifetime of worthless procedures.

This is the point of the nonsense, I would like to say my own views on the language. Why design a new language? The reason is no more than two, or the old language is really unbearable, or for the domain design of the special language. I'm not going to talk about the latter one, because if you don't have a specific field of knowledge, it's never going to work (like SQL never comes from a poorly-run database), and basically it's not a question of language design. So this series will only focus on the former-that is, designing a common language. Universal language also has its own "domain", but too much, so it was diluted. Throughout history, you've got a person who has done only a small amount of work to design a language, and if he's not systematically educated in the language of programming, it can only make a crap. For example, go is one of them-although his father is very good, but does not include the "design Language" thing.

So in 21st century you have to do a language that is not satisfied with all the common language, so you want to do it yourself. What is the unsatisfactory embodiment? For example, the reason for C # may be that his father is not handsome enough, for example, the reason for C + + may be that their IQ is too low hold not live, for example, the reason for Haskell may be that the use of people too little to recruit people, For example, the reason for C is that it is impossible to complete people and abstract so no Linus level of people will write C language, but you can not recruit Linus, in short, there are a variety of reasons. However, in terms of excluding the IQ factor of the user, there are several languages I still appreciate--c++, C #, Haskell, Rust and Ruby. If I were to rank the world's languages, the top five would be the five, though it might be hard to decide between them. But even so, there are some of these languages that make me uncomfortable, so I always want to do a new language (for myself). ), the evidence is--"look at my blog."

So. What is the good of a good language and what is the embodiment of it? All along, people feel that only the library easy to use, language will be easy to use. In fact, this is completely reversed the causal relationship, if there is no good grammar, how can you write a useful library? To find examples is also very simple, just compare Java and C # is enough. C # 's library works well and is inseparable from the ability to express his language, such as LINQ (, to Xml,to sql,to parser,etc), such as WCF (for ease of use only), such as WPF. Can Java write these libraries? Hard to write or can write, but you will find that you do not have the means to use them in a very convenient way, in fact, this is due to the Java grammar garbage caused. This is the time to look up at the five languages I have listed above, and they are characterized by the fact that the library is particularly cool because of the syntax.

Of course, this does not require all people to learn language to be able to write a library. The distribution of the programmer is the same as the pyramid structure, the library let a few people write it, most of them, even if you do not have to learn so much, unless you want to be the writing library of those. However, recently there is a very bad atmosphere, that is, some people feel that a language difficult to themselves can not be "easy" to become a writer of the library, began to say he here is not good there, specifically who I will not call, we all know, hehe ah.

Good language, in addition to the library to write and easy to use, there are two important features: easy to learn, easy to analyze. About easy to learn this, in fact, is not to say that you can learn casually look, but said, as long as you master the doorway, many unknown characteristics you can guess. This has a grammatical consistency problem in it. Grammatical consistency is a problem that is easy to ignore because all the errors caused by bad grammatical consistency are particularly obscure and difficult to see. Let me give you a few examples here to build this concept.

The first example is our popular C language pointer variable definition:

int A, *b, **c;

I believe a lot of people have been trapped by this stuff, so many textbooks tell us that when we define a variable, the last of those asterisks should be written in front of the variable to avoid misunderstanding. So a lot of people would think, why design like this, it is obviously to dig a hole to let people jump down. But in fact, this is a good example of grammatical consistency, and as to why he is a pit, the problem is elsewhere.

As we all know, when a variable B is a pointer to an int, the result of the *b is an int. Define a variable int A; It also means "define a is an int". So let's take a look at the above variable declaration: int *b;. What the hell is this talking about? In fact, the real meaning is "define *b is an int". This "definition and use consistent" approach is actually what we want to promote. The C language function definition parameters are separated by commas, and the invocation is separated by commas, which is good. Pascal's function definition parameters are separated by semicolons, and the invocation is separated by commas, which is less consistent.

See here you may say, how do you know the C language his father thinks so? I think that if he didn't think so, the estimate would not be bad, because there is one more example:

int F (int a, int b);

Int (*f) (int a, int b);

This is also a "definition and use consistent" example. In terms of the first line of code, how do we look at "int F (int a, int b);" What about the wording? In fact, like the above, he said, "The result of defining F (A, b) is int". As to what A and B are, he also tells you that the definition of a is int,b also int. So equivalently, the following line is also "the result of the definition (*f) (A, b) is int". The function type is also not allowed to write parameter names, but we encourage you to write the parameter names so that Visual Studio IntelliSense lets you list the parameter names when you Knock "("), you see the prompts, and sometimes you don't have to go back to the source code.

There is a final example of the "consistent definition and use" of the C language, and this example is also wonderful:

int A;

typedef int A;

Int (*f) (int a, int b);

typedef int (*F) (int a, int b);

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.