Replies: 3 comments
-
|
This is not really an "issue", so moving it to a "discussion". Maybe backing up from lexical file scope to what I'm really thinking about. In es as in other shells, running a single command with output directed to some file is concise and nice, no matter the command. But let's look at the following C function: To recreate that in es, we need to: This involves:
None of these seem ideal. Unfortunately I don't see a way to make the following work as a "lexically-scoped" redirection, either. There's just no real difference between the A somewhat more tenable alternative could look something like the following. It's a little new syntax (both the Another side benefit of a more "robust" real-fd pool which tracks filenames where it knows them: internal error messages could include the file name in more cases. This would be particularly useful in the |
Beta Was this translation helpful? Give feedback.
-
|
As another example of where this is a problem, I'm realizing now that my |
Beta Was this translation helpful? Give feedback.
-
|
I continue to tumble this idea in my head trying to get it shiny. Here's what I have so far. I want to be able to open a file (with a mode specified, either in Internally, this should be fairly easily achievable with an array/list of structs looking something like When The externalization story would be the big question, though. Initially I pictured this as a special kind of binding, so that instead of assignment syntax like So, for maximum flexibility with minimal "magic", a filehandle should probably just be a kind of
Fitting an opaque filehandle into existing syntax might look something like the following dodge: This would create no new syntax, and would only require one novel parsing behavior, which would be making closure-parsing handle So that all seems very pleasantly straightforward. However, a troubling scenario would be something like since the script would get There would be fewer edge cases to worry about with fresh syntax -- but what would that be? Ideally it would be something not specific to file handles, so that it could be generalized. I am imagining something that specifies a function to fetch/generate a new handle, as well as arguments to that function. Like, I don't know, I think I need to keep tumbling this idea in my head for a bit. The core idea still seems sound and useful to me, though. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
(#239 got me really hopped up on the idea of "lexical scope all the things!" Forgive me :)
Es has a couple of places where it opens a file and holds its descriptor, hidden from user code, for some long-ish period of time. The most obvious example of this is the file handling for shell input, which got that whole big mailing-list discussion early on; even if the shell input is on stdin, messing around with stdin doesn't mess around with shell input.
See the following script:
Even when run in a way that explicitly sets the shell input as stdin, the right thing happens -- setting stdin to
wowfiledoesn't muck up the input that the shell is reading:Another example is the prior, pre-#65, behavior for writing to history without readline. This was a performance hack, I think, and it's not too great a loss that it's gone, but it's still notable that the shell runtime had this file descriptor that the user couldn't get at modulo
/proc/fdshenanigans.I think these are evidence that hiding file handles (that is, descriptors) in es would have utility. How would you pull these examples out to the shell level -- as we did with history in #65 and I would like to do with shell input in #178 -- and keep that file-handle hiding? The immediate answer, as usual, ought to be lexical scope.
Given es already has a layer of abstraction between user fds and real fds, this doesn't seem so crazy. There's some prior art with lexical file scope being added after dynamic scope was the only default, in Perl.
There would be a lot of questions to answer. Say with the following script
questions would be:
Would we want to change the existing
%openfilebehavior to create a lexical file scope within the block, or create a new construct for this that could let us do the more explicitlet (fd = <={%write-to my-output}) {...}? I tend to prefer improving the existing shell features, rather than adding new ones, and changing the existing stuff to lexical scope should result in no change in almost all cases. (We'd need to add an escape hatch so that we could keep dynamic scope -- or maybe just make sure to keep theexec {> foo}escape hatch that exists now.)How would we solve the -- to coin a term -- "upward fdarg" problem? For the
%write-historyexample, the lexical binding would need to be on the outside of the function, so when opening the history file the file handle would need to be passed "up" to that binding. How would these handles/fds be represented when printed in a%closure? How can they be correctly inherited in the environment by subshells? For lifetime management, we'd need to do some kind of fd-reference tracking like the GC does now. Is anFd *a type ofTerm *or akindofTree *-- or could we model it some other way?Maybe these questions are signs that this is not the best idea. But it seems to me that there's something here; I am certainly not very happy with how quickly file descriptor use becomes dizzying as it gets even a little bit advanced.
Beta Was this translation helpful? Give feedback.
All reactions