Skip to content

improve Unsafe#27

Merged
garyb merged 2 commits into
purescript:masterfrom
brainrake:better-unsafe
Mar 23, 2015
Merged

improve Unsafe#27
garyb merged 2 commits into
purescript:masterfrom
brainrake:better-unsafe

Conversation

@brainrake
Copy link
Copy Markdown
Contributor

  • add char as discussed in Char constructor is unsafe #24, but stricter, so char "c" can be eg. safely replaced with character literals (eventually), since it only accepts strings of length 1
  • re Data.String.Unsafe is very unsafe #25 make all unsafe functions throw an exception right away on bad input, instead of returning invalid values and maybe causing hard-to-debug errors later on

Comment thread src/Data/String/Unsafe.purs Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have a superfluous full stop (.) at the end of this sentence.

@garyb
Copy link
Copy Markdown
Member

garyb commented Mar 22, 2015

I see what you mean now, I thought you were talking about using purescript-exceptions before. I like this, we should probably do it anywhere unsafe functions have the possibility to result in error occurring away from the call site.

@davidchambers
Copy link
Copy Markdown
Contributor

I like this, we should probably do it anywhere unsafe functions have the possibility to result in error occurring away from the call site.

Sounds like a good idea to me.

@brainrake
Copy link
Copy Markdown
Contributor Author

Exactly, I was going to suggest that but first I went looking for such occurences.
Throwing new Error will show a backtrace, and including the function name in the exception string will make it even easier to debug.
As far as I can tell, the use case for unsafe functions is the situation when you want to forego proving safety or handling failure. We should make sure that even unsafe functions are unsafe in a controlled manner, and won't crash the code anywhere else but when they're called, and crash with an exception.

@brainrake
Copy link
Copy Markdown
Contributor Author

eg Prelude.unsafeIndex just returns undefined
doc:

Note: this function can cause unpredictable failure at runtime if the index is out-of-bounds.

It should throw an exception and the documentation should say so. It will be somewhat slower though.

@brainrake
Copy link
Copy Markdown
Contributor Author

I sort of see how unpredictable failure can be a good deterrent from using unsafe functions though.

@brainrake
Copy link
Copy Markdown
Contributor Author

Array.Unsafe uses unsafeIndex, an incomplete pattern match, and Data.Maybe.Unsafe.fromJust

@brainrake
Copy link
Copy Markdown
Contributor Author

We should also think about the interaction with Exceptions, ie catchException will probably catch exceptions inside unsafe pure code it calls.

@brainrake
Copy link
Copy Markdown
Contributor Author

Good to merge though?

@garyb
Copy link
Copy Markdown
Member

garyb commented Mar 23, 2015

👍 thanks!

garyb added a commit that referenced this pull request Mar 23, 2015
@garyb garyb merged commit bba91ee into purescript:master Mar 23, 2015
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 this pull request may close these issues.

3 participants