Last update October 21, 2004

DWiki /
Dynamic Closures



Difference (previous author) (Change, Edit, normal page display)

Changed: 49,123c49
(From DWiki)
包装机械
折纸机
热收缩机
旋盖机
灌装机
打码机
封口机
打包机
包装机
剥标机
电源
稳压电源
应急电源
开关电源
逆变电源
UPS电源
电源模块
模块电源
交流电源
直流电源
不间断电源
EPS应急电源
净化电源
变频电源
Wire Mesh
Wire Cloth
Welded Wire Mesh
Welded Wire Mesh
wire mesh
wire cloth
Stainless steel wire mesh
Welded wire mesh
Metal Wire Mesh
Brass Wire Mesh
Knitted Wire Mesh
copper wire mesh
Silver wire mesh
nickel wire mesh
Hexagonal Wire Mesh
Black Wire Cloth
Diamond wire mesh
iron wire mesh
Wire Mesh
Wire Cloth
Welded Wire Mesh
Welded Wire Mesh
租房
北京租房
搜房
房屋出租
北京房屋出租
房屋租赁
北京房屋租赁
北京租房信息
北京租房网
注册公司
公司注册
注册美国公司
注册英国公司
注册香港公司
注册离岸公司
注册海外公司
写字楼,写字楼租赁
律师
律师事务所
法律咨询
法律顾问
标志设计
包装设计
设计
ci设计
vi设计
广告设计
平面设计
(From DWiki)

Deleted: 125,130d50





麻将机 起重机 成人用品 可视电话 电力猫 无线上网卡 电梯 叉车 轮胎 电子仪器 汽车配件 涂料 化工机械 干燥设备 颜料 钛白粉 塑料制品 阀门 纯水设备 医用电子 电动自行车 保险柜,保险箱 减震器 生物识别 防伪技术 防火材料 印刷机 耐火材料 照明 制冷 涂装 电镀 表面处理 床上用品 继电器 工艺品 机床 电子秤 电路板 干燥剂 弹簧 纸箱 丝印 纳米焊条 升降机 反光材料 打标机 温湿度计 热处理设备 冷水机 包装设计 压缩机 手机窃听器 电话变声器 针孔摄像机 振动筛 空气净化 吸塑制品 伺服电机 机电设备 超声波设备 电阻 活性炭 实验室设备 玻璃器皿 耗材 集成电路 连接器 电容器 节能灯 断路器 电动机 密封件 电动工具 办公家具 热水器 金属丝网 磁性材料 铝制品 蓄电池 锁具 锂电池 塑料玩具 电动车 防盗器 印刷机械 太阳能热水器 变送器 标准件 仪器仪表 推土机,压路机 绝缘材料 电线电缆 稳压器 硒鼓,深圳硒鼓,墨盒,深圳墨盒,色带 充气床 数码电话 钢管 风机 锅炉 变频器 润滑油 离合器 狐臭 糖尿病 电容 灰指甲 近视眼 脱毛 净水器 蜂蜜 紧固件 制服 健身器材 太阳能热水器 婚纱摄影 LED 酒店 水表 锅炉 打包机 痔疮 发电机 深圳装饰 切割机 深圳印刷 深圳条码 激光设备 照排机 餐具 IP超市计费系统 网络电话 VOIP网关 IP电话 软交换 GK 网络电话 治疗脱发 治疗脱头发 网络电话卡 VOIP VOIP网络电话 脂溢性脱发 脱发特效药 生发产品脱发产品 治疗白发 毛发移植 头发移植 自体毛发移植 皮肤病 伟哥 高血压 交换机 注册香港公司 补品 增高 壮阳 青春痘 化妆品 治脚气 鱼鳞病 白癜风 语音网关 ip超市 话吧 ip公话超市 集团电话 保健品 板蓝根 防盗门 监控 包装机 电缆 门禁 瓷砖 电机 耳机 卫星电视 监视器 变压器 对讲机 传感器 电子元器件 刀具 地毯 竹炭 深圳机票 胶袋,深圳胶袋 胶带,深圳胶带 机票 光端机 voip交易网 脱发治疗网 深圳机票 快递|速递公司 视频会议 汽车租赁 贴纸 饮水机 不锈钢 水晶 融资 电脑维修 出国 开关电源 模具 纳米 污水处理 物业管理 移民 幕墙 五金 私家侦探 六合彩 深圳机票 视频点播 速递公司 国际货运 报警器 茶叶 实木地板 签证 考勤机 深圳酒店

A form of closure which employs dynamic scope. Closures are a powerful technique in which callbacks can retain implicit scope during the "call back", for example.

No more littering your code with extra context parameters and casting...

For example:

void apply_all (int table[], delegate (int) dg) {

  for (int i = 0; i < table.length; ++i) {
    dg(table[i]);
  }
}

void test () {

  int mytable[] = [1, 2, 3];
  int offset = 4;
  void func (int val) {
    printf("%u,",val+offset);
  }
  apply_all(mytable,func);
}

This feature is not without its pitfalls. In the above example, "offset" is in both the dynamic and lexical scope of "func", so it works. If, however, the code were structured such that "offset" fell out of dynamic scope during execution for some reason, the technique will fail.

For example, consider this modification:

delegate (int) create_func () {

  int offset = 4;
  return delegate (int val) {
    printf("%u,",val+offset);
  }
}

void test () {

  int mytable[] = [1, 2, 3];
  apply_all(mytable,create_func());
}

Now "offset" is no longer in dynamic scope, so its value is not defined. The stack location of "offset" will likely be overwritten by "apply_all's" local variable "i".

For a highly relevant (and recommended) discussion of dynamic versus lexical scope from the Lisp/Scheme world, see DynamicScoping on the c2 wiki.

Walter has indicated that he has a plan for a runtime check to catch this type of error.


(From DWiki)


FrontPage | News | TestPage | MessageBoard | Search | Contributors | Folders | Index | Help | Preferences | Edit

Edit text of this page (date of last change: October 21, 2004 12:15 (diff))