Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Nice summary. It leaves me wonder, though: is the philosophy the same as in Unix world? as in, little independent tools that do one and just one thing. Would those tools also need to be modified to add compatibility with the same typing system that is used by the shell?

Or it follows the opposite mentality and the sell comes with batteries included?

I'm thinking of some arbitrary example, like downloading a JSON and parsing it to obtain some nested sub-property. In Bash world you would use the shell to execute at least something like `wget` to do the download part, and then run `jq` to parse the JSON and select the desired property. Both of `wget` and `jq` are independent programs, installed separately, with their own input & output channels, that you combine together by using the shell itself.

How would this work with PowerShell? (feel free to use some other example that might be better suited)



Powershell commands (called "commandlets" or "cmdlets") are little .net (or .net core) programs/methods which accept and return either objects or arrays of objects, rather than plain text.

powershell offers cmdlets which let you sort and filter these result objects based on object property values rather than sorting on plain text values.

Obviously when printing to stdout or stderr, THAT bit is plain text, but until that very last step of displaying in the terminal, powershell results are objects.

So, that gives you a form of type safety, which isn't strictly possible in text-only shells. Powershell uses reflection to inspect the objects since the exact type of a response may have never been seen before. You can write your own cmdlets which return your own types and you can modify types returned by cmdlets using operators like 'select'. So, it's type-safe but not like everyone is used to. Powershell cmdlets always return objects (or errors, I guess), but the structure of those objects isn't always known until runtime. You can still use those never-before-seen types with the standard operators like sort and select, too.

Powershell also offers output conversion cmdlets which let you output to CSV or a textual table or a list, which is helpful when the next step in your pipe chain is a tool that expects textual input or a csv file. One can also write their own output formatters, of course.

In those ways, powershell and nushell appear to have the same goals. I haven't looked at nushell any more closely than it would take to notice this, so there may be other similarities I haven't noticed, yet. I'm sure there are many differences that I haven't noticed yet, as well.


Thank you very much, it's now clearer to me how it works.

Regarding the objects themselves, the way you describe them makes me think of Javascript objects & arrays: you don't need to know the exact shape of an input object, as long as it has the specific properties that you want to work on. Same for arrays: regardless of their content, the language allows you to use the standard ops like map() or find() (and it's then a matter of you providing adequate functions to access the contained elements)


In powershell you would use built ins.

To download json over rest you would use Invoke-RestMethod or irm.

To convert json to objects you would use ConvertFrom-Json.

To select properties from objects you would use Select-Object or select.

Here is a concrete example that I wrote for the rustlings install script: https://github.com/rust-lang/rustlings/blob/main/install.ps1...

Note that I used Invoke-WebRequest instead of irm here.


It looks like: `curl <url> | jq .tag_name` Note: curl & jq are independent programs (as long as `curl` produce json text with tag_name in each, the command works, no matter other changes).

If ConvertFrom-Json & Select-Object were not builtin, how much information about each other would they need to cooperate? Can an unrelated change in ConvertFrom-Json's output break corresponding Select-Object command? How much are they intertangled?


Curl to python is actually what happens in the Linux install script [0].

To your point, all cmdlets adhere to an interface so they always return and receive typed data. They don’t really need to know anything except that they’re receiving or giving powershell objects.

Non built in commands (native binaries) just pipe a stream of bytes just like any posix shell.


ConvertFrom-Json converts json text into a PowerShell object with properties derived from the json.

Select-Object (in this instance) selects a property from the object.

An unrelated change in ConvertFrom-Json wouldn't change it's basic nature which is to convert json into something you can manipulate with other PowerShell cmdlets.


And then I guess the shell itself has mechanisms to easily attach or register new commandlets, to allow for easily add custom object processing?

I really like the consistency that those commands have: ConvertFrom-<ConverterFormat>.

No built in converter for the format you need? I'm just speculating here, but I guess you just need to implement this or that Interface and there you go.


Yes you can write your own cmdlets in either C# or powershell that you can install via modules [0]

You can also just write a function in your profile.

[0] https://docs.microsoft.com/en-us/powershell/scripting/develo...


You can define your own function and use it in your pipeline yes. You can have your function take pipeline input just like the built in cmdlets.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: