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

Allow default arguments #130

Closed
skx opened this issue Apr 8, 2023 · 1 comment · Fixed by #131
Closed

Allow default arguments #130

skx opened this issue Apr 8, 2023 · 1 comment · Fixed by #131

Comments

@skx
Copy link
Owner

skx commented Apr 8, 2023

I came across this example, recently:

(define*
 ;; function name: hi
 ;; a is needed
 ;; b has default argument 32
 ;; c has default argument "hi"
 (hi a (b 32) (c "hi"))
 ;; just returning the arguments
 (list a b c))

(list
 (hi 1)
 (hi :c 332 :a 3)
 (hi 3 2 1))

I'm not sure about the named-arguments, used when calling a function, but allowing default values for parameters seems nice and the syntax is trivial to handle.

@skx
Copy link
Owner Author

skx commented Apr 8, 2023

skx added a commit that referenced this issue Apr 9, 2023
We typically define a function like this:

     (set! hello1 (fn* (name)
       (print "Hello %s" name)))

Here we say there is a parameter "name" which must be supplied, and that
is later used.

If we instead say that parameters can be lists we can allow the first
value to be the name, and the second a default value if nothing is
actually passed, like so:

     (set! hello2 (fn* ( (name "World") )
        (print "Hello %s"  name)))

These could be used like so:

     (hello1 "Steve")  ; "Hello Steve"

     (hello2 "Steve")  ; "Hello Steve"
     (hello2)          ; "Hello World"

I've not yet experimented with supplying defaults in the non-final
arguments, because that could screw things up.  But for simple cases
like the one above it seems to work.

Once complete this will close #130, however I need to add test-cases..
@skx skx closed this as completed in #131 Apr 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant