runtime系统的Cello
runtime系統(tǒng)的Cello
通過充當(dāng)一個現(xiàn)代的、功能強(qiáng)大的runtime系統(tǒng),Cello使許多以前在C中不切實(shí)際或笨拙的事情變得簡單,例如:
通用數(shù)據(jù)結(jié)構(gòu)
多態(tài)函數(shù)
接口/類型類
構(gòu)造函數(shù)/析構(gòu)函數(shù)
可選垃圾回收
例外情況
反思
而且,由于Cello與標(biāo)準(zhǔn)C無縫地協(xié)同工作,所以您可以獲得其他所有的好處,例如出色的性能、強(qiáng)大的工具和廣泛的庫。
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));/?Iterateoverindicesusing"range"?/foreach(iinrange(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做為一個有趣的實(shí)驗(yàn),看看C語言看起來像是被砍掉的極限。除了作為一個功能強(qiáng)大的庫和工具箱外,對于那些想探索C語言的人來說,應(yīng)該很有趣。
它是如何工作的?
建議閱讀一個指針庫來了解Cello是如何工作的。也可以瀏覽一下源代碼,聽說它是相當(dāng)可讀的。
它能用于產(chǎn)品嗎?
最好先在業(yè)余愛好項(xiàng)目上試用Cello。Cello的目標(biāo)是產(chǎn)品化之前試用,但因?yàn)樗且粋€怪物,它有著相當(dāng)?shù)钠嫣睾拖葳?#xff0c;如果在團(tuán)隊(duì)中工作,或者在最后期限,那么C++等語言就有更好的工具、支持和社區(qū)。
有人用Cello嗎?
有人已經(jīng)嘗試過它,據(jù)說,沒有一個引人注目的項(xiàng)目使用它。Cello太大了,如果新的C項(xiàng)目想要便攜和易于維護(hù)的話,它是一個很不錯的依賴庫。
總結(jié)
以上是生活随笔為你收集整理的runtime系统的Cello的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 将5g做到世界顶级
- 下一篇: matrix_multiply代码解析