Skip to content

Destructuring and Function Overloading #2537

Answered by scauligi
danielgomez3 asked this question in Q&A
Discussion options

You must be logged in to vote

Hy doesn't (natively) have function overloading like that, but—just as you can with Python—you can implement something similar by using Python's own match/destructuring:

(require hyrule [case])

(defn process-data [#* args]
  (match args
    [] (print "No data provided")
    ["success"] (print "Operation completed successfully")
    ["error"] (print "Error occurred")
    [_] (print "Unknown status")
    [status-keyword message]
      (case status-keyword
        "success" (print "Operation completed successfully with message:" message)
        "error" (print "Error occurred with message:" message)
        else (print "Unknown status"))))

;; Example usage
(process-data)
(process-data "suc…

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@danielgomez3
Comment options

@Kodiologist
Comment options

@Kodiologist
Comment options

Answer selected by danielgomez3
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants