-
Notifications
You must be signed in to change notification settings - Fork 10
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
Questions about design of some parts #12
Comments
I had initially written |
For the other version of import Exception # NOTE: "Exception", not "Exceptional"
ArgumentError.exception("error message")
|> if exception?(myvar) do
%{message: msg} -> msg
else
myvar
end ☝️ Very close to the suggested version, but no extra lib required 😄 EDIT: actually, it would be more like this (👇) in a pipe, I guess. ( import Exception # NOTE: "Exception", not "Exceptional"
ArgumentError.exception("error message")
|> exception?()
|> case do
%{error: msg} -> msg
myvar -> myvar
end |
Heh, it would, however doesn't /me wishes that ArgumentError.exception("error message")
|> case do
Exception.exception?(exc) -> :blah
val -> :success
It is implemented pretty easy if you wanted to add in such a macro? ^.^ Also, I've been busy making a typed ML-like language in elixir Macro's as well as typing elixir itself if you are curious (very much a bit of a grown-hack, needs revamping, but eh): Can check my latest post at https://elixirforum.com/t/typed-elixir/1388/14 for an example |
Ooooh I see what you mean now. Indeed, that would be handy! That's going in the roadmap 🎉
Nice! I'm glad to see a few of these popping up. We all need more ML-family languages in our lives! |
/me loves ML languages, especially OCaml Typing Elixir itself is... interesting though I must admit. ^.^ |
Yep, definitely still interested in if_exception handling bodies instead of functions. :-) I'd really like something like this: blah
|> if_exception val do
nil
else
val + 1
end Or something like that, be able to name what is being passed in and have the do and else bodies actual bodies instead of piping into a function, could keep the form without the named variable as it is now, that way fully backwards compatible (although adding constant values support there would be nice, and still be backwards compatible). :-) Right now I keep doing this, which is not quite as pretty, though it works to give a name to the value (which I'm ignoring in this specific case, but still). ^.^ Perms.verify(conn, %Perms.Account{action: :index, id: account_id, type: k})
|> if_exception do
case do
_ -> nil
end
else
case do
_ -> val
end
end |
Although, to be honest, you know how Hmm... brb... |
And I've now made that specific code into: Perms.verify(conn, %Perms.Account{action: :index, id: account_id, type: k})
|> case do
match_exception -> nil
_ -> val
end It matches if it is an exception struct (you can still put I'm not fully satisfied with it, but it has definitely cleaned up a good bit of code actually, so I'll keep it. I can PR it if you want ( |
That syntax would actually make a good exception test too, another idea, I've not made this (yet) but it would be easy to make: blah
|> exception_case do
exception = %MyException{} = exc -> blorp(exc)
exception = %AnotherException{} -> nil
exception = exc-> dwoop(exc) # Any other exception
value -> value
end Or perhaps some other keyword than I guess that is already pretty close to |
Was there a follow-up on this? |
A couple questions on design decisions or perhaps requesting new helper functions.
First, why does
safe
return a fun instead of branching at the point of call to be used as normal. Right now at https://github.com/expede/exceptional/blob/master/lib/exceptional/safe.ex#L86 it is doing a large branching and anapply
call for every call. If instead if it (or a new name) were made a macro, then it could be fully inlined instead as a calling site, so you could do this for the examples in the README.md instead:This could help readability and especially the usage while piping. Easily implementable as well.
Second,
if_exception
and such have a similar setup, they take functions, where if they were macro's then theif_exception
last example could become:Also easily implementable, just stuff the do body in a case statement (maybe with a default to continue passing unmatched exceptions) and the else as normal. Could even optimize it so if the do body macro list has only one element and it is not keyed on a
:->
then it could be a simple pass-through, so these would both work:Or a configurable variable name could be used like:
And of course the else clause could be optional if it is just a pass-through anyway.
And so forth on the other parts, just to help reduce the typing required and increasing readability by removing all sorts of things like
.()
from many calls.The text was updated successfully, but these errors were encountered: