71
submitted 9 months ago by [email protected] to c/[email protected]

A friendly programming language from the future.

you are viewing a single comment's thread
view the rest of the comments
[-] [email protected] 2 points 9 months ago

I would even have said that both throwing and catching should be pure, just like returning an error value/handling should be pure, but the reason for the throw/returning error itself is impure. Like if you throw and ioerror it's only after doing the impure io call, and the rest of the error reporting/handling itself can be pure.

[-] [email protected] 1 points 9 months ago

Sounds good,

but would the preferred way be to use a wrapper type, which holds either the data or the error and avoid exceptions completely?

[-] [email protected] 1 points 9 months ago

Pure functions should be referentially transparent; you should be able to replace them with whatever value they evaluate to without changing the semantics of your code.

Throwing is referentially impure: what value do you get from calling x => throw new RuntimeException()?

Instead, functional languages prefer to return a tagged union of the value or the error.

this post was submitted on 16 Dec 2023
71 points (85.9% liked)

Programming

16999 readers
109 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 1 year ago
MODERATORS