Objective-C, about which I wrote the previous article, is a language with many problems and inconveniences. But we have to write applications for iOS using this language. Therefore, I have developed another language, which generates Objective-C code, and where you can simply use an Objective-C class and a C function. You can also use a generated class in an Objective-C code. It’s therefore no problem to develop in two languages at the same time. I have named this language “Objective-D”.
It should be admitted that it is not a complete product. I developed this language for myself and for my own needs, so there are still a lot of bugs and defects. The language was developed in Haskell, but at the time I didn’t have a great deal of experience in Haskell. For that reason the source code is not great, and needs to be refactored.
I took the Scala syntax as a basis, because Scala syntax is very laconic and I like it. Class declaration is thus very simple:
I like the named parameters in Objective-C, because they increase the readability of the code.
However, sometimes parameter names are just superfluous words, and in Objective-D you can omit them.
These classes allow Objective-C classes to be used in Objective-D. They describe Objective-C classes or C structs.
It’s awful that Objective-C has no generics and uses duck typing. I have developed generics and a static type system in Objective-D.
You are not able to indicate the data type of a variable or a function. It can be computed using the initialization value.
You can extend a class with one class and with many traits, but a trait is not a simple interface. It can contain fields and definitions of functions; only a constructor cannot be defined in a trait.
You can define a struct and functions in the struct. A struct is generated into a C struct and C functions.
At present this only works with the special names of functions (add, sub, div, mul). But I plan to realize the following:
The following examples show the syntax for blocks:
The data type of the item can be computed using the generics, so it can be omitted.
You can use built-in expressions and multi-line strings. Look at the example:
Tuples are very useful to unite some values without declaration of a special class. Sometimes it’s very convenient to use a tuple as a type of a return value of a function.
Lazy calculations can be very useful. Such values will be calculated only in the first call.
Pattern matching is a big theme in the functional programming. It allows more laconic and clearer code to be created.
I like enums in Java, so I have realized something similar in Objective-D.
I feel there is lack of a package system in Objective-C, and I don’t like prefixes of classes.
But a prefix is necessary in Objective-C, so you can define the prefix for your package in Objective-D in the package object. This prefix will be added to generated classes and files. In the package object you can also define common imports for all classes inside the package.
I started the development of Raildale using Cocos2D and Objective-C. Later, I stopped using Cocos2D and replaced it with OpenGL. If I had used OpenGL at the beginning I could have chosen C++ instead of Objective-C.
After some time I recognised the first problem with Objective-C: it has no enums. By this I mean enums with linked properties as in Java, or something else that might replace it. I devised some methods to solve this, but a lot of code was required. I tried to use C macros but I didn’t manage to find a really good solution.
I decided to develop a code generator for enums. Development of a code generator is a normal practice in language-oriented programming, which helps to reduce the amount of code (I have written an article about this). This was the first part of Objective-D:
When I was choosing the platform for the development of the generator, there were three possibilities: JetBrains MPS, Scala and Haskell. I decided on Haskell because Haskell has a great framework for parsing (Parsec), and it seemed interesting and I hadn’t tried it yet. I must say Haskell is a really great language.
I developed enums very quickly. But I thought it would be good to write simple immutable classes in a laconic syntax. I therefore developed the generator for this case. Then I added the ability to write simple functions in classes, and continued adding features during development of Raildale. I also developed the plugin for AppCode, which supports the syntax highlighting and some types of refactoring. So far I have spent 230 hours developing Objective-D.
I would be glad if someone would try out Objective-D, and I will help fix problems as soon as I can. If someone likes the project and wants to help develop it I would also be very happy.
Download Objective-D and unarchive;
Create project in Xcode;
Copy path ObjDLib in your project and add m-files in your target;
Add build phase in your target (Editor->Add Build Phase->Add Run Script Build Phase) and call ObjD file in the path bin:
Create a file with od extension and write an Objective-D code. For example:
Build the project. h-file and m-file will be generated in the same folder with od-file. Add these files to your project.
You can also download the plugin for AppCode.
Generate to Java- I’m going to port Raildale to Android;
Eradicate static functions and fields - objects is a better decision;
Add more checks to make development more safe;
Default values for parameters of functions.