nushell / nushell/nushell.github.io

Confusing example on script definition order

Offen
#1,163 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
TypeScript
Sterne
258
Forks
561
Ø Merge
3 Std. 20 Min.
Gemergte PRs (30 T.)
15

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der im Issue verlinkten Dokumentationsseite zu scripts und prüfe die Beispiele zur Definitionsreihenfolge zusammen mit der umgebenden Erklärung der Skriptausgabe. Aktualisiere das Beispiel oder füge einen Hinweis hinzu, damit die unterschiedliche Ausgabe eindeutig erklärt wird, wenn der Aufruf verschoben wird. Erledigt ist die Aufgabe, wenn die Dokumentation die Definitionsreihenfolge korrekt demonstriert, ohne Neulinge irrezuführen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Bereich
documentation
Issue-Typ
Dokumentation
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.