特性列表

语法级别

  1. try-catch-finally
try {

} catch {
  
}

// 处理所有类型错误(Error)
try {

} catch (err) {

} finally {

}

// todo: 是否需要多级catch
try {

} catch (e : DivZeroError) {

} catch (e : ParseNumberError) {

} catch (e : Error) {

} finally {

}

 // 只处理指定类型的错误
try {

} catch (e : DivZeroError) {

} finally {

}
  1. instanceof / is
val cat = new Cat();
if (cat instanceof Tiger) {
  // xxx
}

val v1 = nil;
val b = v1 instanceof Cat;  // false
val b2 = v1 is Cat;  // false
  1. switch
switch (value) {
  case constVal1: {
    break;
  }
  case constVal2: {
    return;
  }
  default: {

  }
}

// todo:高级switch??
val v1 = switch (value) {
  case constVal1: "text"->length();
  case constVal2: 2;
  default: callSomeFunc();
}
  1. import for
import com.southstate.FileUtils for { listFiles as getFiles, listDirs };  // listFiles和listDirs必须是静态方法

class MyClass {
  public MyClass() {
    val utils  = new FileUtils();
    val bEq = utils->listFiles == getFiles;  // true
  }
}
  1. 结构话字符串
`我爱${name}已经${years}年了`

Annotations 一个参数都不写会默认有一个value

export @interface Path {
  val value;
  val name = 'jerry';
}

Iterable / Iterator

  • 所有可foreach迭代对象都必须继承Iterable接口
class ChildrenIterator implements Iterator {
    
  public ChildrenIterator() {
    // xxx
  }

  public val hasNext() {
    // return xxx;
  }

  public val next() {
    // return xxx;
  }
}

export class Children implements Iterable {

  public val getIndex(index) {
    // xxx
  }

  public void setIndex(index, value) {
    // xxx
  }

  public val keyIterator() {
    return new ChildrenIterator();
  }
}

String

Array

  • 构造
val arr;
arr = new Array(10, 'text');  // 10个 'text'
arr = new Array(100);  // 100个 nil
arr = new Array();  // 空的
arr = [0, 1, ['text'], { children: [] }, 'text'];
  • for / foreach
val arr = new Array(10, nil);
for (val i = 0; i < arr->length(); i += 1) {
  Console->print(i);  // 10个nil
}

val arr = [1, 2, 3];
foreach (val index, value in arr) {
  Console->print(index);  // 0 1 2
  Console->print(value);  // 1 2 3
}
  • length
  • add
  • remove
  • insert
  • replace
  • join
  • map
  • forEach
  • filter
  • matchOne
  • matchAll
  • find
  • findLast
  • findIndex
  • findLastIndex
  • sort
  • indexOf
  • lastIndexOf
  • reverse
  • reduce
  • [index] / getIndex(index)
  • ImmutableArray 不可更改数组

Range

  • 构造
val r = [0...5];      // 0,1,2,3,4
val r = [0...5, 2];  // 0 2 4

val start = 0;
val r = [start...(10 + start), 3];  // 0 3 6 9

[0...5, 2]->length();  // 3
r->length();  // 3
  • for / foreach
val arr = [0...5, 2]->toArray();  // [0, 2, 4]

foreach (val i, v in [0...10, 2]) {
  Console->print(i);  // 0 1 2 3 4
  Console->print(v);  // 0 2 4 6 8
}
  • 不能更新,构建后即为死值
  • length
  • map
  • forEach
  • reverse
  • reduce
  • toArray
  • [index] / getIndex(index)

Table

  • 有序keys??
  • 构造
val table1 = new Table();
val table2 = {};

val children = [];
val user = {
  name: 'Tom',
  'the-age': 23,
  male: false,
  children,
  goWalk: () => 'xxx'
};
  • keys
  • hasKey
val tbl = {
  name: '张三', 
  weight: nil
};

// true
val b = tbl->hasKey('name');
// true
val b = tbl->hasKey('weight');

// true
val b = !!tbl.name;
// false
val b = !!tbl.weight;
  • values
  • clear
  • removeKey
  • forEach
  • [key] / getIndex(key)
  • toArray

Errors

  1. TypeError
  2. DivZeroError
  3. RangeError
  4. SyntaxError
  5. ParseError
  6. TargetError
  7. Error

Reflect

  1. classOf 获取对象或类型的Class描述
val clazz = Reflect->classOf(UserModel);
  1. expands 静态方法,扩展Object的原始功能
    • expands(object, toExpandMethods, tagValue)
    • expands(object, toExpandMethods) // tagValue nil

用法 val object2 = Reflect->expands(object, toExpandMethods, tagValue);

object是原始对象,不能是基础类型(boolean, integer, float, nil, function),toExpandMethods是一个Table,不能为nil,每个key-value是函数(如getAge),会为object生成同名扩展函数,使用object调用这个名称(getAge)时候,会映射到扩展函数(getAge)。tagValue是标签对象,供扩展函数使用。本函数返回object

扩展函数如下,getAge是函数名称,函数回调传参分别是:refobjecttag是最初传入的tagValue

val toExpandMethods = {
  getAge: (ref, tag, arg1, [...argN]) => {
    // xxx
  }
};

例子:

val arr = [1, 2, 3, 4, 5];
val toExpandMethods = {
  reverseJoin: (ref, tag, gapStr) => {
    return ref->reverse()->join(gapStr);
  }
};
val tagValue = nil;
Reflect->expands(arr, toExpandMethods, tagValue);
val str = arr->reverseJoin('-');  // 5-4-3-2-1
  • expandsNew 等价于调用new对象,然后调用expandObject
val toExpanded = {
  reverseJoin: (ref, gapStr) => {
    return ref->reverse()->join(gapStr);
  }
};
val args = [[1, 2, 3, 4, 5]];

val arr = Reflect->expandsNew(Array, toExpanded, tagV, args);
val str = arr->reverseJoin('-');  // 5-4-3-2-1
  1. newProxy 为接口类型创建代理对象
interface HttpApi {
  @HttpGet('/user/${id}')
  val getUserById(@Path('id') id);
}

class HttpRequest {
  private val callback;
  public void setCallback(callback) {
    this->callback = callback;
  }
}

val httpConfig = {
  base: 'https://smu.com/',
  httpGet: (url) => {
    val req = new HttpRequest();
    // do real http get
    // req->callback(response);
    return req;
  }
};

val httpProxy = Reflect->newProxy(HttpApi, (ref, tag, method, args) => {
  log('call before');
  
  val path = method->annotations[0]->value;
  
  foreach (val i, argAnnos in method->argsAnnotations) {
    foreach (val j, argAnno in argAnnos) {
      if (argAnno is Path) {
        val pathVar = '${' + argAnno->value + '}';
        path = path->replace(pathVar, args[i]->toString());
        break;
      }
    }
  }

  val url = tag.base + ;
  return tag.httpGet(url);  // 发起get请求
  
  log('call after');
}, httpConfig)

val req = httpProxy->getUserById(101);
req->setCallback((response) => {
  // xxx
});

Class 类型的描述信息

package a.b;
class UserModel {
  private val name;
  public final val gender;
  public val getName() {
    return name;
  }
}
val clazz = Reflect->classOf(UserModel);

clazz->shortName;  // UserModel
clazz->name;  // a.b.UserModel

clazz->targetPublicFields;  // 当前类本身的公共属性
clazz->targetFields;  // 当前类本身的所有属性,包括私有和保护的
clazz->allPublicFields;  // 所有的公共属性,包括继承来的
clazz->allFields;  // 所有的属性,包括继承来的和私有保护的

clazz->getPublicField(name);  // 获取指定的公共属性
clazz->getField(name);  // 获取指定属性

clazz->targetPublicMethods;  // 当前类本身的功能函数
clazz->targetMethods;  // 当前类本身的所有函数,包括私有保护的
clazz->allPublicMethods;  // 所有的公共函数,包括继承来的
clazz->allMethods;  // 所有的函数,包括继承来的和私有保护的

clazz->getPublicMethod(name, argCount);  // 获取指定函数名称和参数个数的公共函数
clazz->getMethod(name, argCount);  // 获取指定函数名称和参数个数的函数

clazz->constructors;  //
clazz->getConstructor(argCount);  // 

val superClazz = clazz->getSuperClass();  //
val myInterfaces = clazz->getTargetInterfaces();  //
val allInterfaces = clazz->getAllInterfaces();  //

Method

name;
argCount;
accessible;
annotations;
argsAnnotations;  // val someFunc(@AnnA() @AnnB arg)
invoke(ref, args);s

Field

Promise

async / await

工具类

Math

RegExp

Date

Json

  • parse
// 普通类
val user = Json->parse(UserModel, 'json string');

// Table类
val table = Json->parse(Table, 'json string');

// 默认Table类
val table = Json->parse('json string');

// 自定义Table类
val myTable = Json->parse(MyTableClass, 'json string');
  • stringify
// 
val json = Json->stringify(tableOrObject, includekeys, space);

// 
val json = Json->stringify(tableOrObject, includekeys);

// 
val json = Json->stringify(tableOrObject);

未完成

  • 多继承检查
  • poplar.lang下类无需导入??? ✅
  • 要不要支持 val type = User; val user = new type('Jerry');??? ❌
  • 去掉 new Error() 的栈占用
  • 函数不定参数
  • System->loadLibrary()
  • [...keys]解构
  • a is Number; a is Float; a is Integer; a is Object; a is Boolean; a is Function; 基础数据类型
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,980评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,422评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,130评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,553评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,408评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,326评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,720评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,373评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,678评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,722评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,486评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,335评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,738评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,009评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,283评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,692评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,893评论 2 335

推荐阅读更多精彩内容