-8

So ChatGPT seems convinced that in Zig there is something called shorthand syntax for single-expression functions and it works like this:

fn add(x: i32, y: i32) i32 => x + y;

But when I try to use that I get:

error: expected ';' or block after function prototype
fn add(x: i32, y: i32) i32 => x + y;
                           ^~

I cannot find any documentation about single-expression functions, so is ChatGPT hallucinating here?

Does zig have special syntax for single-expression functions?

The ChatGPT transcript (for completeness):

me: does zig have implicit return?

While Zig does not have implicit return, it does have a shorthand syntax for functions that contain only a single expression. In this case, you can omit the curly braces and use the => operator to specify the return value. For example, the add function could be rewritten using this shorthand syntax like this:

fn add(x: i32, y: i32) i32 => x + y;

In this example, the function body consists of a single expression x + y, and the => operator is used to specify the return value of the function.

me: since which version of zig is that supported?

The shorthand syntax for single-expression functions using the => operator was introduced in Zig version 0.7.0, which was released on October 6, 2020. This version introduced a number of new language features and improvements, including the => operator for single-expression functions.

Before Zig 0.7.0, all functions in Zig had to be defined using curly braces and an explicit return statement. However, the => operator provides a more concise syntax for simple functions with a single expression, making the code easier to read and write.

It's worth noting that the => operator is not a replacement for the traditional syntax, but rather a shorthand for simple functions. In Zig, you can still use the traditional syntax with curly braces and an explicit return statement for more complex functions that require multiple statements or control flow.

hkBst
  • 2,818
  • 10
  • 29
  • Try asking it again. It might well give you the right answer (accidentally) eventually. – Alex Reinking Mar 12 '23 at 16:08
  • 1
    When I tried to experiment with ChatGPT, it told me "The current version of Zig is 0.7.0, released on April 9, 2021"... – sigod Mar 12 '23 at 19:29
  • 1
    My understanding is that ChatGPT-3 finished training in late 2020. Note that Zig 0.7.1 was released on 2020-12-13. If ChatGPT indicates that the current version of Zig is v0.7.0, this should not come as a surprise. It would seem that it is a bad idea to ask ChatGPT technical questions that must have different answers after the time of training, and a really bad idea to ask about a fast-moving in-development language that hasn't reached v1.0 yet. – ad absurdum Mar 15 '23 at 02:47
  • @adabsurdum, I am aware of ChatGPT's learning horizon and not surprised that it thus thinks an old version is current. You seem to be suggesting that Zig used to have this feature ("must have different answers") without actually saying it. Do you have any knowledge about this or is this mere speculation. Indeed my whole question is about whether ChatGPT is correct about the past or not, but I could not find any evidence that it is. – hkBst Mar 19 '23 at 18:31
  • 2
    I was not suggesting that Zig ever had this feature. You can look at all of the grammars for Zig from v0.1.1 up to the current master branch by consulting the Zig documentation page and selecting the version at the top. [Here is a link to the grammar for v0.7.1](https://ziglang.org/documentation/0.7.1/#Grammar). The only place `=>` has ever been used in any of the grammars is in the syntax for switch prongs. For good measure, here is the [v0.7.0 grammar](https://ziglang.org/documentation/0.7.0/#Grammar). – ad absurdum Mar 19 '23 at 19:18

1 Answers1

4

No, it does not.

Here is a link to the official YACC-compatible grammar for Zig: https://ziglang.org/documentation/0.10.1/#Grammar

The token => (EQUALRARROW) appears only in Zig's switch-case syntax and nowhere else. Zig does not have any other compact syntax for single-expression functions, either.


If you ask ChatGPT the same question multiple times, you will find it gives you contradictory responses. For instance, it told me that Zig does have implicit return, which it does not.

Don't use large language models for anything you can't personally validate.

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86