Writing an API that is syntactically valid in multiple programming languages

I’m planning to write a single API that is syntactically valid in most major programming languages (to the greatest extent possible, so that only minimal amounts of code will need to be re-written when moving from one language to another). I think the simplest way to do this would be to write a group of functions that consisted entirely of function calls and variable assignments, since the syntax for function calls is almost exactly the same in all major programming languages.

Of course, I’d need to write “wrapper” functions for if-statements and while-loops, since the syntax of each of these is different in each programming language.

Here’s a proof-of-concept for one “polyglot” function that should work in most programming languages:

//JavaScript syntax
function proofOfConcept(){
    printSomething("This is just a proof of concept."); //the function printSomething will
    //                      need to be implemented separately in each programming language.
}

//Java syntax
public static void proofOfConcept(){
    printSomething("This is just a proof of concept."); //the function printSomething will
    //                      need to be implemented separately in each programming language.
}

//Python syntax
def proofOfConcept():
    printSomething("This is just a proof of concept."); //the function printSomething will
    //                      need to be implemented separately in each programming language.

//C syntax
void proofOfConcept(){
    printSomething("This is just a proof of concept."); //the function printSomething will
    //                      need to be implemented separately in each programming language.
}

Would this be a useful design strategy, or is there a better way to do this? I think this would be a useful strategy for developing libraries in multiple programming languages, althogh it would require a small number of “language-specific” functions to be written for each target language.

12

First question – is it useful? Not particularly. Trying to make one API that fits a multitude of languages is often a mistaken approach. There are several different programming types listed in the example:

  • C – classic procedural programming
  • JavaScript – multi-paradigm tending to functional and object oriented
  • Java – Object Oriented.
  • Python – multi-paradigm tending to object oriented

The way of thinking in each of these languages is particular to that language. One can try to write procedural code in all the languages – completely ignoring polymorphism and just reverting everything to C like struts, but it would not integrate well with others. It would also forgo all the advantages of writing in a higher level language. Passing around dumb objects to static procedures defined somewhere – that is so 1980s.

The question of why really shows up. What practical use would this serve? If I am writing C code, I’m writing C code. Polyglot shops exist, but they are not the norm (it is much easier to hire 6 programmers for one language and get stuff done than it is to hire 1 programmer in each of 6 languages and get the same amount of stuff done). Rarely does one find a programmer that is writing Java and C at the same time where the perceived advantage of having one library API that fits both Java and C could be of a very small advantage.

What you are talking about is implementing a domain specific language (the printSomething(String) of the polyglot API) as a runtime environment in a multiple languages. This isn’t impossible, but isn’t that useful either. This is harder than one imagines – Python’s scope is based on indentation, Javascript’s semicolon is a statement separator as opposed to the statement terminator in Java and C (and non-existent in python).

This isn’t an impossible task, but really, it isn’t that useful.

3

The Katahdin programming language was designed to solve this problem. Its documentation includes this example, which combines Fortran and Python functions in one file:

import "fortran.kat";
import "python.kat";

fortran {
    SUBROUTINE RANDOM(SEED, RANDX)
        INTEGER SEED
        REAL RANDX
        SEED = 2045*SEED + 1
        SEED = SEED - (SEED/1048576)*1048576
        RANDX = REAL(SEED + 1)/1048577.0
        RETURN
    END
}

python {
    seed = 128
    randx = 0
    for n in range(5):
        RANDOM(seed, randx)
        print randx
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật