runtime系统的Cello
通过充当一个现代的、功能强大的runtime系统,Cello使许多以前在C中不切实际或笨拙的事情变得简单,例如:
通用数据结构
多态函数
接口/类型类
构造函数/析构函数
可选垃圾回收
例外情况
反思
而且,由于Cello与标准C无缝地协同工作,所以您可以获得其他所有的好处,例如出色的性能、强大的工具和广泛的库。
https://github.com/orangeduck/Cello
Examples
#include “Cello.h”
int main(int argc, char** argv) {
/* Stack objects are created using
“$” */
var i0 = $(Int, 5);
var i1 = $(Int, 3);
var i2 = $(Int, 4);
/* Heap objects are created using
“new” */
var items = new(Array, Int, i0, i1, i2);
/* Collections can be looped over */
foreach (item in items) {
print(“Object %$
is of type %$\n”,
item, type_of(item));
}
/* Heap objects destructed via Garbage
Collection */
return 0;
}
#include “Cello.h”
int main(int
argc, char** argv) {
/* Shorthand $ can
be used for basic types */
var prices = new(Table, String,
Int);
set(prices, $S(“Apple”), $I(12));
set(prices, $S(“Banana”), $I( 6));
set(prices, $S(“Pear”), $I(55));
/* Tables also
support iteration */
foreach (key in prices) {
var val = get(prices, key);
print(“Price of %$ is %$\n”, key, val);
}
return 0;
}
Articles
Learning Resources:
Installation
Cello World
Quickstart
Common
Queries / Pitfalls
Articles about its creation and internal workings:
Best
Improvements of Cello 2.0
A Fat Pointer
Library
Cello vs C++
vs ObjC
Benchmarks
Garbage
Collection
More
Examples
#include “Cello.h” int main(int argc, char** argv) { var items = new(Array, Int, $I( 8), $I( 5), I ( 20 ) , I(20), I(20), I(15), $I(16), I ( 98 ) ) ; / ∗ I t e r a t e o v e r i n d i c e s u s i n g " r a n g e " ∗ / f o r e a c h ( i i n r a n g e ( I(98)); /* Iterate over indices using "range" */ foreach (i in range( I(98)); /∗Iterateoverindicesusing"range"∗/ foreach(iinrange(I(len(items)))) { print(“Item Range %i is %i\n”, i, get(items, i)); } /* Iterate over every other item with “slice” */ foreach (item in slice(items, _, _, $I(2))) { print(“Item Slice %i\n”, item); } return 0;}
#include “Cello.h”
/* Define a
normal C structure */
struct Point {
float x, y;
};
/* Make it
compatible with Cello */
var Point = Cello(Point);
int main(int
argc, char** argv) {
/* Create on Stack
or Heap */
var p0 = $(Point, 0.0, 1.0);
var p1 = new(Point, $(Point, 0.0,
2.0));
/* It can be shown,
compared, hashed, etc…
**
** p0: <‘Point’ At 0x000000000022FC58>
** p1: <‘Point’ At 0x00000000004C7CC8>
** cmp: 1
** hash: 2849275892l
*/
print(“p0: %KaTeX parse error: Undefined control sequence: \np at position 1: \̲n̲p̲1: %\ncmp: %i\nhash: %ul\n”,
p0, p1, $I(cmp(p0, p1)), $I(hash(p0)));
/* And collected by
the GC when out of scope */
return 0;
}
F.A.Q
为什么会有这种情况?
把Cello做为一个有趣的实验,看看C语言看起来像是被砍掉的极限。除了作为一个功能强大的库和工具箱外,对于那些想探索C语言的人来说,应该很有趣。
它是如何工作的?
建议阅读一个指针库来了解Cello是如何工作的。也可以浏览一下源代码,听说它是相当可读的。
它能用于产品吗?
最好先在业余爱好项目上试用Cello。Cello的目标是产品化之前试用,但因为它是一个怪物,它有着相当的奇特和陷阱,如果在团队中工作,或者在最后期限,那么C++等语言就有更好的工具、支持和社区。
有人用Cello吗?
有人已经尝试过它,据说,没有一个引人注目的项目使用它。Cello太大了,如果新的C项目想要便携和易于维护的话,它是一个很不错的依赖库。