Yesterday on Habré, I published my article about language-oriented programming, in which I described what problems exist in this area and how JetBrains MPS solves them. Today I want to show real examples of how the code looks in MPS, with a few comments. We will look at the language for working with databases, the language for creating a web interface, and our javascript. The code snippets are taken from a bug tracker application codenamed Charisma written in MPS.
Here is a code snippet in our language for working with databases. User entity is defined here:
')
As you can see, we have special field types. Simple fields with primitive values, association fields. Fields have many options: fields can be made indexable, hashed (to store the password), specify default values ... Associations are one and two-sided, some of them have an event handler. As you can see, the code is clear and concise. The construct in which everything is contained is an extension of the Java class.
When something changes in the database, you often want to somehow react to it. For this, we have the language of business rules. Here is an example of a business rule:
This business rule is sent a letter to users when the ishya changed, with which they are somehow connected. We indicate under which condition it is called, and what it does in this event. The language for business rules is the DSL in which it is inserted.
Let's now look at how to write html templates. Here is the template used when adding a new comment:
It uses html, extended constructs for a shorter record of frequent things. Of course, writing regular tags is also possible, but a more domain-specific entry looks much nicer.
With each template, we associate client-side and server side-side controllers, where events that the template generates are processed. Here is an example controller:
As you can see, languages extend each other, one language is often inserted into another. In fact, languages are used in the same way that we usually use libraries in languages like Java. Thanks to this, we can greatly raise the level of abstraction.
Learn more about MPS here:
jetbrains.com/mps
And that is not all. We are looking for a senior developer in the MPS project, in this role you will be able to participate in the development of MPS, and in creating languages on it. Details in the vacancy in my profile.