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

Implement proper functions and get rid of setref? #968

Open
andychu opened this issue Jul 14, 2021 · 3 comments
Open

Implement proper functions and get rid of setref? #968

andychu opened this issue Jul 14, 2021 · 3 comments

Comments

@andychu
Copy link
Contributor

andychu commented Jul 14, 2021

  • setref is sort of a hack for "return values"
    • it was originally based on the declare -n nameref mechanism, but it diverged, because that turned out to use too much dynamic scope. It would simplify the code to get rid of it.
    • Also I wanted to change proc p(:out) to proc p(out Ref), but maybe we don't need that
  • Originally I was going to cut scope and just have procs, and not funcs. So setref was sort of an "escape hatch"
  • But now we have builtin sub for e.g. ${.myproc arg1}, and I think functions can be recast in terms of that mechanism.
    • the stdout can be the return value (like elvish I think), AND it can be captured efficiently in process.
    • It will just have the normal f(x, y) syntax, with arguments passed on stdin and return values on stdout? This is different than "builtin sub" which only takes string arguments, which precludes \0 inside the strings, which we DO WANT.
    • builtin sub Implement fast command sub: ${.myproc arg1} and @{.myproc 'a' "$b"} #799
@andychu
Copy link
Contributor Author

andychu commented Jul 18, 2021

Argument for setref: So the user can implement something like read :x

Error Handling For Functions

We have the issue mentioned in #942 -- how do you check errors?

I want to use something like if try, not introduce a try/catch ...

@andychu
Copy link
Contributor Author

andychu commented Jul 20, 2021

Idea: functions defined on top of procs. Value semantics only!

proc myfunc {
  msgpack read :args
  msgpack write [[42, 'foo']]  # return value  
}

# defines an interface wrapper of the SAME NAME
# shopt --set type_assert can check them?
func myfunc(x Int, y Int) Int [msgpack]

  • So a func can wrap
    • a proc
    • an external process
    • corpocesses

Maybe a better block-like syntax:

func myfunc {
  arg x Int "docstring"
  arg y Int "docstring"
  returns z List[Int] "docstring"

  format = 'msgpack'  # can be JSON too
  
  # by default it wraps a proc of the same name
  # how to make it wrap a process or coprocess?
  command = %(foo.py myfunc)
  command = %(coproc foo.py myfunc)
}

Another idea: types as lazy arg lists?

arg x [Int]
arg y [Int]
returns z [List[Int]]      # unfortunately template arguments and lazy arg lists both use []

@andychu
Copy link
Contributor Author

andychu commented Oct 30, 2023

We are going to do value.Place instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant