V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xuanwu
V2EX  ›  随想

从木兰的 1[0] = [0] 有感编程语言语法设计的舍与得

  •  
  •   xuanwu · 2021-03-03 11:26:06 +08:00 · 614 次点击
    这是一个创建于 1142 天前的主题,其中的信息可能已经有所发展或是发生改变。

    原文:《从木兰的 1[0] = [0] 有感编程语言语法设计的舍与得

    前几天在用木兰写 同步多个 git 仓库的脚本 时,碰到个熟悉的报错: TypeError:'NoneType' object is not subscriptable。于是照例试图将此报错添加到 之前的测试用例

    先试了 print(nil[0]),报语法错误,暂不深究,改为如下:

    a = nil
    print(a[0])
    

    如期报错。

    如果 true 值,类似报错:TypeError:'bool' object is not subscriptable

    再试了整数类型:

    a = 1
    print(a[0])
    

    如期报类似错:TypeError:'int' object is not subscriptable

    顺便一试 print(1[0]),结果,输出了 [0]

    @_@ 咋回事??

    一时没头绪,照例看它生成的 Python 语法树:

     [略] 
            args=[BinOp(
                left=Num(
                  n=1
                  lineno=1
                  col_offset=7
                )
                op=Mult()  <---- 乘法
                right=List(
                  elts=[Num(
                      n=0
                      lineno=1
                      col_offset=9
     [略] 
    

    这才想起半年前发现木兰对 省略乘号的乘法支持,自然也适用于整数与数组、字符串的乘法。在木兰交互环境演示如下:

    > 长=4;宽=3
    > 4 长 + 3 宽
    25
    > 2[0]
    [0, 0]
    > 3"鹅"
    鹅鹅鹅
    

    回头看此语法,仅当数在表达式前时成立,似乎没什么不妥。但对比下面这两个用法,就觉得有些别扭。

    > a=1
    > a[0]
     😰 TypeError:'int' object is not subscriptable,见第 1 行
    > 1[0]
    [0]
    

    对比 Python 下的行为,看着就比较“一致”:

    >>> a=1
    >>> a[0]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'int' object is not subscriptable
    >>> 1[0]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'int' object is not subscriptable
    

    空值和布尔值也是类似。

    感觉木兰似乎在这点上是违反了一个“潜规则”(请教相关编程语言术语是什么呢?):表达式中的变量可以替换为一个常量,而且得到的反馈应与变量为此值时相同。比方 a=3; b=4; a*3+b*43*3+4*4 两个表达式的结果相同。

    木兰选择支持更接近数学表达式的效果,比如 3a+4b,但用过其他编程语言的用户就需要克服“思维惯性”来适应。算是有舍有得的一个例子吧。

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1381 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:47 · PVG 07:47 · LAX 16:47 · JFK 19:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.