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

explicit print as nu no longer automatically prints output of every line #1208

Closed
wants to merge 1 commit into from
Closed

explicit print as nu no longer automatically prints output of every line #1208

wants to merge 1 commit into from

Conversation

Inveracity
Copy link

Change

This PR updates the script examples to explicity print output.

Reason

As per this comment #1163 (comment), Nu no longer automatically print output of every line.

closes #1163

@fdncred
Copy link
Collaborator

fdncred commented Jan 13, 2024

I'm not sure it works like that now. This seems to work fine.

def greet [name] {
  ["hello" $name]
}
❯ greet moon
╭───┬───────╮
│ 0 │ hello │
│ 1 │ moon  │
╰───┴───────╯

However, this is really how I think it's meant to work currently.

def greet2 [name] {
  ["hello" $name]
  $name
}
❯ greet2 moon
moon

You'll notice that ["hello" $name] doesn't print out in greet2. This is because we only return/print the last item.

But if you try greet3, you'll see that both print again.

def greet3 [name] {
  echo ["hello" $name]
  echo $name
}
❯ greet2 moon
╭───┬───────╮
│ 0 │ hello │
│ 1 │ moon  │
╰───┴───────╯

moon

@Inveracity
Copy link
Author

Ah I see

When I initially started out writing functions I called them multiple times in a script and was confused as to why I only got the output of the last function call

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

greet "one"
greet "two"

only outputs

╭───┬───────╮
│ 0 │ hello │
│ 1 │ two   │
╰───┴───────╯

without the explicit print

It wasn't until I found that comment I linked to, that it made sense.

I can close this if it's not relevant to have the explicit print, but personally I think it would help some newcomers as I ran head first into this 😄

@Inveracity
Copy link
Author

It literally just struck me that it's an implicit return 🤦 now I understand. Started learning Nu yesterday haha

@Inveracity Inveracity closed this Jan 13, 2024
@Inveracity Inveracity deleted the print-greet-example branch January 13, 2024 17:50
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 this pull request may close these issues.

Confusing example on script definition order
2 participants