oilshell / oil

Oils is our upgrade path from bash to a better language and runtime. It's also for Python and JavaScript users who avoid shell!
http://www.oilshell.org/
Other
2.78k stars 150 forks source link

typeof / reflection / assign results of '=' #1997

Closed dezren39 closed 1 week ago

dezren39 commented 1 week ago

hi i want to be able to determine what type my typed arg is, how do i do that?

also i can't reference func/proc is there a way to do this? i feel like it might be nice to be able to assign them or pass them directly into functions, and for functions to be able to switch on typeof(x) and switch if command/func/proc they can go eval or if string do something else, etc.

can i make procs/functions dynamically? have a proc/function that takes a proc/function makes a proc/function and returns it? thinking like, currying basically.

proc blocks are cool, i'm curious why there is only one and how i would go about making an array of blocks or assign a block to a variable? sometimes i want to reuse a block i'd pass into a proc, would that be a caret?

i found this worked below, closest i could get, but idk if i could pass in values if i needed to.

proc hi { echo "hello" }
var z = ^(hi)
ysh ysh-0.22.0$ proc hi { echo "hello" }
ysh ysh-0.22.0$ var l = ^(hi)
ysh ysh-0.22.0$ proc x(;y) { eval (y) }
ysh ysh-0.22.0$ x (l)
hello
ysh ysh-0.22.0$ = x
(Command)   <Command 0x7f1875415508>

^ how do i get "Command" into a variable? i'm less interested in the pointer at least right now, but if i can get the whole thing i can parse it myself

oh and i got ysh to throw with this poor choice of mine

ysh ysh-0.22.0$ func y (;x) { return = x }
Traceback (most recent call last):
  File "/home/andy/git/oilshell/oil/bin/oil.py", line 34, in _cpython_main_hook
  File "/home/andy/git/oilshell/oil/bin/oils_for_unix.py", line 170, in main
  File "/home/andy/git/oilshell/oil/bin/oils_for_unix.py", line 137, in AppBundleMain
  File "/home/andy/git/oilshell/oil/core/shell.py", line 1034, in Main
  File "/home/andy/git/oilshell/oil/core/main_loop.py", line 219, in Interactive
  File "/home/andy/git/oilshell/oil/osh/cmd_parse.py", line 2739, in ParseInteractiveLine
  File "/home/andy/git/oilshell/oil/osh/cmd_parse.py", line 2580, in _ParseCommandLine
  File "/home/andy/git/oilshell/oil/osh/cmd_parse.py", line 2504, in ParseAndOr
  File "/home/andy/git/oilshell/oil/osh/cmd_parse.py", line 2515, in _ParseAndOr
  File "/home/andy/git/oilshell/oil/osh/cmd_parse.py", line 2463, in ParsePipeline
  File "/home/andy/git/oilshell/oil/osh/cmd_parse.py", line 2340, in ParseCommand
  File "/home/andy/git/oilshell/oil/osh/cmd_parse.py", line 2218, in ParseYshFunc
  File "/home/andy/git/oilshell/oil/osh/cmd_parse.py", line 1309, in ParseBraceGroup
  File "/home/andy/git/oilshell/oil/osh/cmd_parse.py", line 2705, in _ParseCommandList
  File "/home/andy/git/oilshell/oil/osh/cmd_parse.py", line 2690, in _ParseCommandTerm
  File "/home/andy/git/oilshell/oil/osh/cmd_parse.py", line 2504, in ParseAndOr
  File "/home/andy/git/oilshell/oil/osh/cmd_parse.py", line 2515, in _ParseAndOr
  File "/home/andy/git/oilshell/oil/osh/cmd_parse.py", line 2463, in ParsePipeline
  File "/home/andy/git/oilshell/oil/osh/cmd_parse.py", line 2416, in ParseCommand
AssertionError: (Token
  id: Id.ControlFlow_Return
  length: 6
  col: 14
  line: (SourceLine line_num:118 content:"func y (;x) { return = x }\n" src:(source__Interactive))
  tval: return
)
FATAL: couldn't import from app bundle '/etc/profiles/per-user/user/bin/ysh' (1)
Stripping the oil.ovm binary may cause this error.
See https://github.com/oilshell/oil/issues/47

maybe this is related to some of my questions? #691 maybe related too? #1849 and https://oilshell.zulipchat.com/#narrow/stream/384942-language-design/topic/block.20arguments.20as.20third.20.2F.20fourth.20arg.20group

dezren39 commented 1 week ago

ok i found this https://github.com/oilshell/oil/blob/master/doc/ref/chap-builtin-func.md#functype

dezren39 commented 1 week ago

closing this because the answer of the primary question is 'use type()'

i'm guessing that generating functions is unavailable, other than maybe by codegen a file and then ? import ? or exec it? it would be nice if i could make closures etc.

andychu commented 1 week ago

Oh thanks for finding the crash! Let me make a new issue for that one.

Yes the function is type()

There are no closures right now... It may be possible to introduce an object that you can __call__ like Python

Feedback based on example code is very welcome, thanks!