0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Zig v0.17でArray multiplication operatorが廃止された

0
Posted at

はじめに

Zigのバージョンアップ恒例(?)の破壊変更祭の中には、Array multiplication operator (**)も含まれる。
以下のようにして使ってたもの。

const result = [1]u8{0} ** 4; // [4]u8{0, 0, 0, 0}になる

リリースノートでは代わりに@splat()を使えとある

-        var result = [1]u8{0} ** window_size;
+        var result: [window_size]u8 = @splat(0);

数値の配列であれば、これで問題ないかもしれないが、以下のようなケースは対応していない

const x: [12]u8 = "Hey!" ** 3; // "Hey!Hey!Hey!"になる

これは繰り返しの元が[4:0]u8のスライスであるため、@splat()で繰り返しできないことに起因している。

回避策

std.simd.repeat()を使えるよと教えてもらった。

test "we have ** at home" {
    const actual: [8]u8 = std.simd.repeat(8, @as(@Vector(3, u8), "ha ".*));
    try std.testing.expectEqualStrings("ha ha ha", &actual);
}

"ha " ** 3で済んでたものが、えらく長くなってしまってはいるが、できることがわかっただけでも安心。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?