Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusing example on script definition order #1163

Open
necabo opened this issue Dec 5, 2023 · 3 comments
Open

Confusing example on script definition order #1163

necabo opened this issue Dec 5, 2023 · 3 comments

Comments

@necabo
Copy link

necabo commented Dec 5, 2023

Issue

I'm currently reading the book which claims that

there is no requirement that definitions have to come before the parts of the script that call the definitions [...]

while showing these scripts as examples of how definition order does not matter:

def greet [name] {
  ["hello" $name]
}

greet "world"
greet "world"

def greet [name] {
  ["hello" $name]
}

This choice of examples is a bit unfortunate in my opinion as, when running both scripts, they have different output.
My understanding is that this is caused by the last expression in a script being printed which evaluates to ["hello" "world"] and nothing respectively.
However, this confused me initially and I would expect this to trip up some other new users as well.

Possible improvements

  1. Changing the script to e.g.
def greet [name] {
  ["hello" $name]
}
greet "world" | null

or

def greet [name] {
  ["hello" $name]
}
let foo = greet "world"
$foo

would result in the same output when moving the command invocation line around but might raise other questions.

def greet [name] {
  ["hello" $name]
}
def greet-world [] {
  greet "world"
}
greet-world

and then moving the definition of greet-world to the top is probably the best example I can think of right now.

  1. Alternatively, the different output could be explained in a small note. I'm not even sure sure if the book explains the last expression of a script being printed up to that point. It's definitely explained in a note for functions but I don't remember reading that the same applies for scripts. I wouldn't rule out that I simply missed that part, though.
@fdncred
Copy link
Collaborator

fdncred commented Dec 5, 2023

The statement, "there is no requirement that definitions have to come before the parts of the script that call the definitions" used to be true but I've seen several instances where order matters. I'm not sure if that means the docs are wrong or if there's a bug in nushell.

@necabo
Copy link
Author

necabo commented Dec 5, 2023

I believe the statement holds for the given example as I would expect the script to error out otherwise.
However, the statement could of course not be true in general. I can't really tell as I'm 2 days into Nushell.

I should add that when I originally encountered this behavior, I also looked for duplicates in the main repository and found this recent issue that was indeed a bug but has been fixed since.

@sophiajt
Copy link
Contributor

sophiajt commented Dec 5, 2023

This isn't an order thing, but rather we no longer automatically print output of every line.

def greet [name] {
  ["hello" $name]
}

greet "world"

Should now be:

def greet [name] {
  print ["hello" $name]
}

greet "world"

And similar with the second example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants