歡迎您光臨本站 註冊首頁

命令式編程語言 Nim 0.20 發布,1.0 還會遠嗎?

←手機掃碼閱讀     admin @ 2019-06-13 , reply:0

Nim 團隊已於上周發布了 Nim 0.20,官方表示這是一個重大更新版本,它包含超過 1000 個 commit,也算是標誌著 1.0 候選版的推出。

Nim 0.20 引入了 1.0 所需的一些破壞性變更,這些變更將會被引入到 Nim 1.0 穩定版中,並且目前也沒計劃繼續引入更多的破壞性變更。

所以 Nim 團隊稱這個版本為「事實上的 Nim 1.0 RC1」。他們還解釋了為什麼不直接發布 1.0,主要是希望讓社區能徹底測試 Nim 0.20,並找出可能是破壞性變更導致的 bug。更重要的是,1.0 的發布意味著 Nim 已到達一個穩定階段,將不再進行任何重大的修改。所以團隊希望在 0.20 基礎上發布 1.0 候選版本,最終才是 1.0 穩定版。

Nim 0.20 新特性

not 永遠是一元操作符


let a = false

# v0.19:
assert not a # Error: type mismatch: got <proc (cond: untyped, msg: string): typed, bool>
assert(not a) # workaround

# v0.20:
assert not a

針對整數和浮點數轉換進行更嚴格的編譯時檢查


# v0.19:
const b = uint16(-1)
echo b # 65535

# v0.20:
const b = uint16(-1)
# Error: -1 can't be converted to uint16
const c = not uint16(0)
echo c # 65535

面向常量和for循環變數的元組拆包


const (d, e) = (7, "eight")
# v0.19: Error: identifier expected, but got '('

# v0.20:
echo d # 7
echo e # eight


let f = @[(51, 10), (23, 25)]

for (x, y) in f: # v0.19: Error: identifier expected, but got '('
  echo x + y
# v0.20:
# 61
# 48

默認情況下對哈希集和表進行初始化


import sets, tables

var s: HashSet[int]

s.incl(5)
# v0.19: `isValid(s)` Error: unhandled exception: The set needs to be initialized. [AssertionError]
# v0.20:
echo s # {5}


var t: Table[char, int]
t['a'] = 10
# v0.19: Error: unhandled exception: index out of bounds [IndexError]
# v0.20:
echo t # {'a': 10}

針對 case 語句提供更友好的錯誤消息


type
  MyEnum = enum
    first
    second
    third
    fourth

proc foo(x: MyEnum): int =
  case x
  of first: 1
  of second: 2
  of third: 3
  of fourth: 4
  else: 99

# v0.19: compiles
# v0.20: Error: invalid else, all cases are already covered


proc bar(x: MyEnum): int =
  case x
  of first: 1
  of third: 3

# v0.19: Error: not all cases are covered
# v0.20: Error: not all cases are covered; missing: {second, fourth}

在迭代期間,表的長度不得更改


import tables

var xs = {1: "one", 2: "two", 3: "three"}.toTable

for x in xs.keys:
  if x mod 2 == 0:
    xs[10*x] = "a lot"
echo xs

# v0.19: {200: "a lot", 1: "one", 2: "two", 3: "three", 20: "a lot"}
# v0.20: Error: unhandled exception: the length of the table changed while iterating over it [AssertionError]

針對索引超出範圍的情況,提供更友好的錯誤消息


let a = [10, 20, 30]

echo a[5]
# v0.19: Error: index out of bounds
# v0.20: Error: index 5 not in 0 .. 2

詳細的更新日誌和更新說明請查看發布公告


[admin ]

來源:OsChina
連結:https://www.oschina.net/news/107407/nim-0-20-0-released
命令式編程語言 Nim 0.20 發布,1.0 還會遠嗎?已經有178次圍觀

http://coctec.com/news/all/show-post-207056.html