13 ms·
Cello – A library that brings higher level programming to C
- krat0sprakhar 9y agoCello seems to make its way to HN quite often. The author (also of buildyourownlisp.com fame) has previously posted his motivations for building libcello on HN: https://news.ycombinator.com/item?id=8800575 https://news.ycombinator.com/item?id=8800575 Another previous discussion: https://news.ycombinator.com/item?id=6047576 https://news.ycombinator.com/item?id=6047576
- ndesaulniers 9y agoWhy not just use C++?
- astrodust 9y agoAs a long-time C++ programmer I can say there's a lot of reasons to not use C++, but still, yeah. What about D? Rust?
- frostirosti 9y agoC++ isn't strictly a superset of C! Which I always found crazy. Some C will not compile for C++.
- YSFEJ4SWJUVU6 9y agoWhy did you find that crazy? They are 2 fully separate languages that have both evolved after the latter (C++) was first introduced.
- EnFinlay 9y agoFor people who aren't professionally familiar with C or C++ it's an easy mistake to make considering how often things refer to "C/C++" like they are a single skill. Example: https://sjobs.brassring.com/TGWebHost/jobdetails.aspx?jobId=342549&PartnerId=25872&SiteId=5512 https://sjobs.brassring.com/TGWebHost/jobdetails.aspx?jobId=...
- jacquesm 9y agoOnce upon a time there was this thing called 'cfront'. Cfront took C++ and turned it into C which you then fed through your compiler. At that time mixing C and C++ was trivial, especially because C++ was still quite simple and the C compiler was the target. But after that things got more complicated. C evolved, several times in fact since that time and the C++ standard evolved as well. Leading to the impression that C and C++ are merely the old and the improved version of C but it is probably much better to think of them as two distinct species that share a common ancestor, where one of the two had some very radical mutations.
- loeg 9y agoIt would be relatively easy for C++ to add some kinds of C compatibility, and they just haven't bothered to. (E.g., the "static" keyword for parameter array sizes.) It's a little annoying for shared headers that have to be used in C and C++ programs.
- elastic_church 9y ago"5 years of Java/Javascript, with emphasis on PHP" Contact Pro IT Recruiting where we know our candidates, Referral bonuses too!
- dfrey 9y agoWhen people use the term "C/C++", I assume that they don't know either language well.
- TorKlingberg 9y agoI wouldn't call them "fully separate". Mixed C/C++ codebases are still fairly common, which works as long as the interface between them is in C.
- astrodust 9y agoMixed C, Objective C and C++ code-bases are still fairly common. That's not to say they're the same language.
- umanwizard 9y agoWell, your argument doesn't really prove anything, as Obj-C is actually much closer to C than C++ is.
- w8rbt 9y agoThey have separate ISO standards. IMO, this makes them fully separate. C - ISO/IEC 9899:2011 C++ - ISO/IEC 14882:2014
- AnimalMuppet 9y agoBut the standards have made some effort at avoiding gratuitous incompatibility, and have even introduced changes to reflect changes in the other standard. Also, much C code is still valid C++. Sure, you can write code that isn't, but I would guess (pulls a number out of nowhere) that 90% of the valid C code is also valid C++. That makes them languages that have separate standards, but not completely independent standards, that share a whole lot of source code. That's something less than fully separate in my book.
- deleted 9y ago[deleted]
- samfisher83 9y agoCan you provide C code that will not compile with c++ compiler?
- msoucy 9y agoSure: struct Foo { int virtual; };
- metaobject 9y agoYou're being pedantic about reserved words?
- spc476 9y agoI had that same problem. I wrote a C library [1] that has a structure field named "class". It's "class" because the protocol being described (DNS) calls that particular field "class" [2]. And I'm not about to pay lip service to an abomination like C++ [3][4]. [1] https://github.com/spc476/SPCDNS https://github.com/spc476/SPCDNS [2] https://github.com/spc476/SPCDNS/blob/ca5052c3d0c3252071a18ebbada808a7b536c860/src/dns.h#L255 https://github.com/spc476/SPCDNS/blob/ca5052c3d0c3252071a18e... [3] I am NOT a fan of C++. [4] But I had to anyway, but I used the C pre-processor to rename the field.
- camgunz 9y agoYeah I super hate this. There's no other language that requires other languages to contort themselves like C++ does. Every time I see `#ifdef __cplusplus` or `klass` my blood pressure spikes.
- astrodust 9y agoHere's a bunch of examples: http://www.geeksforgeeks.org/write-c-program-wont-compiler-c/ http://www.geeksforgeeks.org/write-c-program-wont-compiler-c...
- Chromozon 9y ago
- bobbyi_settv 9y agoIt would be hard for it to be a completely strict superset of C because that would require not introducing new reserved words (like "class") since those end up being legal variable names in C but not C++.
- frostirosti 9y agoWhy not just write another language, that compiles to machine code, but doesn't totally enforce type safety. Call it Iron.
- lacampbell 9y agoAnd make sure Iron has a small group of annoying, hardcore fans who relentlessly disrupts any thread about C or indeed programming in general with their ham fisted advocacy.
- geofft 9y agoThat seems annoying. It's probably a lot better to have a large group of hardcore fans of C who relentlessly disrupt all of programming in general with vulnerability-riddled code. Make sure they're writing code instead of disrupting any discussion threads, that will make sure they're not annoying.
- jacquesm 9y agoC has sharp edges, and buffer overflows are bad, we can all agree on that. Still, there are good ways to advocate a language and there are bad ways. Iron fans are in-your-face to the point that it crossed a line for me in a way that no other language supporters have and I haven't coded in C for a long time now so I don't feel like I have dog in the race.
- jacquesm 9y agoI wonder if the Iron fans realize that they are doing more harm than good the way they go about this. It reminds me of the perl zealot days.
- oblio 9y agoThe Iron fans are doing a bit of harm but that Perl zealotism is one of the things that made it wildly popular in its day. And unlike Perl, Iron development is structured, not "organic" a la Larry Wall/Perl. I'm not an Iron zealot but I do believe that there rarely is such a thing as bad publicity :)
- catnaroek 9y agoCello and C++ are have very different design goals. Cello is essentially a dynamic language implementation on top of C, and uses runtime reflection in places where a C++ programmer would probably prefer compile-time template hackery.
- int_19h 9y agoIdiomatic C++ is not like Cello at all. But it still allows you to do all things that Cello does - and most of them wouldn't be macros then, but classes and overloaded operators on them.
- catnaroek 9y agoSomehow I doubt C++'s built-in object system will allow you to create classes (yes, classes, not objects) and/or alter their vtables at runtime. (I know, vtables are an implementation detail. You still get the idea.)
- int_19h 9y agoNot anymore so than the C one - it's just as static. The point is that building blocks that you get from C++ are higher-level even so - classes, operator overloading, template metaprogramming etc - which should make it possible to implement something like Cello with a lot less effort (and hacks).
- lacampbell 9y agoslow compile times, complex language, sanity
- IshKebab 9y agoCompile times aren't that slow, and the language is barely less complex than this insane hack. In fact C++'s complexity allows libraries to make their use a lot simpler. Consider string concatenation in C++ and C. Which is really simpler? The main reason I can think not to use C++ is that C has a simple stable ABI so you can easily use C libraries from many languages and compilers. But you can always wrap C++ libraries in a C API so that's not a huge concern. Just a pain.
- kbwt 9y ago> Compile times aren't that slow I beg to differ. C++ encourages placing more code than strictly necessary into header files. Even the standard headers such as <algorithm> add considerably to the compile time, and they keep getting larger with every revision of the standard. When you are making incremental changes, it accumulates to a lot of time spent waiting.
- gruez 9y agouse precompiled headers?
- comex 9y agoPrecompiled headers never worked well due to a variety of limitations. C++ modules, on the other hand, will be like precompiled headers done right. If they ever get standardized. They didn't make it into C++17, and the prototype implementations in Clang and MSVC are incompatible with each other, but I guess it'll happen someday…
- gruez 9y ago>Precompiled headers never worked well due to a variety of limitations. want to elaborate? i use MSVC's implementation and never encounter any problems with it. then again, i also don't work on large projects, so i would like some insight.
- feelix 9y agoThat's like saying "Why not just use lisp?" to a python programmer. They're entirely separate languages (albeit it they do share some commonalities, but not as many as you might think). C is my main language and I dabble in C++. I really dislike C++. I love C. I welcome any efforts to add a bit of higher level functionality to C. I have no desire (ever) to switch to C++.
- loeg 9y agoC programmer here. Cello isn't really C. So as long as you're using something that isn't quite C, and you want an object model, I think "why not C++?" is a reasonable question.
- AnimalMuppet 9y agoBut it didn't look to me like Cello was an object model. To me, it felt more like Perl or Python than C++ - more like a dynamic scripting language.
- webkike 9y agoLooks like C to be. Just some macros and function calls.
- khedoros1 9y agoIf someone showed me a sample of the code, out of context, I wouldn't have guessed it was C. Maybe Javascript (with the "var" declarations).
- feelix 9y agoWhy would you say that "Cello isn't really C"?
- orange_fritter 9y agoIt's arguable that extensive use of macros and faux-dynamic-typing violate the ideology of C that makes it simple and predictable.
- jokoon 9y agoA good and valid reason would be compiler availability. C++11, 14 and 17 might not stable for every compiler out there. C++11 and further can be fancy, and not be supported in every possible platform like exotic ones. Bad reasons would be to talk about the gimmicks of C++, but generally it boils down to programmers being taught well about the language, because it's not an easy one to understand fully, as there many weird pitfalls. C++ still has to fix some its flaws, like compile times (this should be improved with modules). So to put it simply, you can still do good work with C because C compilers are just simple and straightforward. When you don't need C anymore (because you only use for it's size and speed, not for the high level), you use something more high level like python. C++ should allow programmers to do everything, but C++ is not perfect and not fully accessible to everyone.
- AnimalMuppet 9y ago> Can it be used in Production? > It might be better to try Cello out on a hobby project first. Cello does aim to be production ready, but because it is a hack it has its fair share of oddities and pitfalls, and if you are working in a team, or to a deadline, there is much better tooling, support and community for languages such as C++. Wow. Straight talk instead of salesmanship. High marks for that.
- scythe 9y agoIIRC Cello doesn't enforce type safety, which means you can foldl but it's not much different from writing foldl in C and using void pointers everywhere. I had thought about trying to make a type-safe version of Cello but I eventually realized that I can't do it in cpp so at that point it became its own language and too much work (I did not write Cello).
- stephen_g 9y agoI'm going to say you're right, given one of the first declarations is typedef void* var; Cello is extremely impressive and could be fun for hobby projects, but for something you'd actually want to use on a real product you'd be better off just creating a new language (it could compile down to C even, like Vala did).
- dingdingdang 9y ago>> (it could compile down to C even, like Vala did). Nim is another take on that particular strategy.
- cvwright 9y agoAnother recent one is Ivory. http://ivorylang.org http://ivorylang.org
- wyc 9y agoThe style reminds me of some J implementations! #define DO(n,x) {I i=0,_n=(n);for(;i<_n;++i){x;}} http://code.jsoftware.com/wiki/Essays/Incunabulum http://code.jsoftware.com/wiki/Essays/Incunabulum
- jnordwick 9y agoI think Arthur Whitney's (in)famous code makes its way to HN about once a year.
- agumonkey 9y ago#A=As #I=it #S=sh #o=o #u=uld A I Sou
- dangerbird2 9y agoThe worst part of that macro is that it is inherently unhygienic: it leaks the variable name 'i' up the scope
- jacquesm 9y agoPresumably there is a #define I int floating around somewhere so that need not be the case (note the capital I inside the curly brace).
- dangerbird2 9y agothe Big "I" is the type, which is a macro parameter. The variable name "i" is not a parameter. If There is another variable "i" in the scope, the loop variable will shadow it in the DO statement. Something like this float i = 10; DO({i+= 10;} 20); will evaluate to this, modifying the "i" declared inside the macro float i_1 = 10; {I i_2=0,_n=(n);for(;i_2<_n;++i_2){i_2 += 10;}}
- jacquesm 9y agoHm, in the linked page there is a typedef long for I. typedef long I; #define DO(n,x) {I i=0,_n=(n);for(;i<_n;++i){x;}} (I added a comma here after the }, 20 is the x parameters) DO({i+= 10;}, 20); expands to: typedef long I; {I i=0,_n=({i+= 10;});for(;i<_n;++i){20;}}; The '20' presumably should have been some kind of function call or action to repeat and the 'n' should have been the count (the reverse from your example). So that would make this: typedef long I; {I i=0,_n=(20);for(;i<_n;++i){ i+= 10; }}; You're still right that it will shadow any 'i' declared outside and so it won't work but it will not overwrite it as far as I can see. You'll just end up right where you started. What I really don't get is if they're going to use _n for the count anyway why not count it down to 0, that way the whole 'i' could be avoided. (you'd still have a problem with _n but that could be overcome with a convention, which of course someone will forget with some nasty bug as a result)
- wybiral 9y agoWhat features make it "higher level"? It looks like syntactic sugar on C code without any real improvements (aside from GC).
- goldbabelfish 9y agoI believe those things are exactly what make it higher level. Unless I'm misunderstanding your question, all that makes a language higher level is the amount of abstraction between the human and the computer.
- khedoros1 9y agoWhat makes [your favorite language] "higher level"? It looks like syntactic sugar on top of assembly language. It's higher level because it provides abstractions that improve the language's expressiveness.
- shakna 9y agoI've used Cello for a few side projects, and it doesn't even really feel like C in the end. Just a few off the top of my head: * No need to specify type. Use var. * Simpler for loops * Inbuilt types for Hash Tables * File types, making file reading much easier * Function types, making it easier to pass functions around * Doctype access * Threads & Mutexes * Format strings * GC (with ability to turn C-types into Cello-types for GCing.) It is fundamentally syntax-sugar, but enough that what you end up with doesn't necessarily look like C at the end. with(f in new(File, $S("test.txt"), $S("r"))) { var k = new(String); resize(k, 100); var v = new(Int, $I(0)); foreach (i in range($I(2))) { scan_from(f, 0, "%$ is %$ ", k, v); show(k); show(v); } } (From: http://libcello.org/learn/file http://libcello.org/learn/file)
- easytiger 9y agoYou are right, it looks different: awful. It is even more cloudy than the syntax of bash.
- ckastner 9y ago> * No need to specify type. Use var. I see types everywhere; they appear to just have been moved from the left-hand side to the right-hand side? eg: var i0 = $(Int, 5); var items = new(Array, Int, i0, i1, i2);
- amaks 9y agoThere are several reasons why people use C, one of them is that the language makes it very explicit what generated code is going to look like. That's one of the reasons why Windows kernel is written in C (https://msdn.microsoft.com/en-us/library/windows/hardware/ff559740(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/hardware/ff..., https://view.officeapps.live.com/op/view.aspx?src=http://download.microsoft.com/download/5/b/5/5b5bec17-ea71-4653-9539-204a672f11cf/KMcode.doc https://view.officeapps.live.com/op/view.aspx?src=http://dow...). Libraries like this obfuscate source code.
- 95014_refugee 9y agoA problem with C in modern usage is that many people believe this to be more the case than is warranted.
- r00t- 9y agoEither the examples are terrible or the library is. I'm leaning towards the latter.
- int_19h 9y agoLooking at the code, a great deal of this is non-portable and/or U.B, so I would hesitate to call it C. And I'm not talking about some hypothetical problems, but stuff that should surface quite soon. For example, this is how stack allocations are made: #define alloc_stack(T) ((struct T*)header_init( \ (char[sizeof(struct Header) + sizeof(struct T)]){0}, T, AllocStack)) So far as I can see, there are basically no alignment guarantees here - the returned pointer to the char array is not guaranteed to be aligned properly for Header (which is a struct of a single void* field), nor is there any attempt to align T inside the array. If things get misaligned, on x86 and x64, it'll work (but possibly much slower than normal), but on e.g. ARM you'll get all kinds of weird things happening.
- ant6n 9y agoApparently ARM allows unaligned loads now (permitted in ARMv6, default in ARMv7). There seem to be some instructions that don't support it, like STM. Also, afaik code needs to be aligned properly, and function pointers need to be aligned properly lest you will switch between arm and thumb mode. Apparently you can tell the ARM compiler to assume all memory accesses are unaligned. Under ARMv8, I think alignment is less of an issue. http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka15414.html http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc....
- hydrocat 9y agoIsn't this something similar to vala ?
- kbart 9y agoHow about performance? As I understand, it uses fat pointers fat pointers and GC, so performance drop is expected. There are not many reasons to use C nowadays beside performance.
- camus2 9y ago> There are not many reasons to use C nowadays beside performance. portability? stable ABI? ... writing something in C make it easy for any other higher level language to link to it. That's why we're not done with C, at all ... it's basically the only serious language out there used to share code among every possible platform or language. Even C++ which is a bit safer in practice is harder to link. I just wished C was a bit safer by default (arrays with bound checking,...)
- maccard 9y ago> Even C++ which is a bit safer in practice is harder to link. Not to go down the C++ evangelist route, but if you want to write libraries in C++ to use with other high level languages, you can wrap the headers in extern "C", and still write C++ as normal in your own code.
- ziotom78 9y ago…unless your library exposes classes/templates, which is probably what the majority of C++ libraries do (e.g., wxWidgets, Qt, Boost, etc.). In this case, creating C bindings is a real pain.
- kungtotte 9y agoThere are many languages with excellent C ABI compatibility with nearly the same portability, as well as some languages that compile down to C, while offering more safety and productivity. So aside from performance and legacy codebases, there's no huge incentive to start a project in C these days. I think those who do it, do it in spite of the alternatives out there.
- 9y ago
- Safety1stClyde 9y agoAs a superstitious C programmer, typedefing (void star) feels like walking on the cracks in the pavement, crossing the path of a black cat, walking under a ladder, or squeezing a lemon under the full moon to me. These kinds of tricks seem very clever at first but there always comes a point when they start to break down. I'd be leery about using union in 2017, but typedefing (void star) is like putting on your underpants outside your trousers, thinking you're superman and jumping out of a window thinking that you can fly.
- simias 9y agoI'm not super familiar with Cello (and I'm not sure I get the point of it overall, are there that many platforms left that you can target from C but not C++?) but in its defense it does seem to implement fat pointers and runtime checks to have a degree of type safety. Not sure how thorough it is but it's not just decaying everything to void pointers behind the scenes. It's a pretty clever hack though, like using setjmp for exception handling. I'm pretty sure I'd never want to use that in production anywhere but it was probably fun to implement.
- crazyhatfish 9y agoDo you use Python in production? It uses setjmp for its exception handling.
- ensiferum 9y agoTake a turd and roll it in sugar it's still a turd. To me C is obsolete and most people who have a "reason" to use C don't really have a real reason other than stupidity. C++ does everything that C does except only better. ;-)
- antirez 9y agoCool. This is a bit extreme but on purpose I think, it is an exploratory project AFAIK. If I had my hands free I would spend my time writing a new C library since I firmly believe that the problems of C are, for the larger part, in its standard library and not in the language itself. C memory model is unsafe but if mediated by a sane library for strings, and if by default you have most of the things where bugs are put (data structures, facilities for parsing, ...) things get a lot simpler and safer.
- zaiste 9y agoHere's the author of Cello talking about the project at PolyConf in 2015: https://www.youtube.com/watch?v=bVxfwsgO00o https://www.youtube.com/watch?v=bVxfwsgO00o « In this talk I dig into the depths of my programming library Cello - a fun experiment to see what C looks like when pushed to it's limits. I'll cover how Cello works internally, some of the cuter tricks used to make it look and feel so different, what is in store for future versions of Cello, and why it is important to push languages to their boundaries. »