ghcjs / ghcjs-base

base library for GHCJS for JavaScript interaction and marshalling, used by higher level libraries like JSC
MIT License
45 stars 67 forks source link

Document how to deal with null and undefined when using pFromJSVal #87

Open louispan opened 7 years ago

louispan commented 7 years ago

AFAIK JSString should never contain null or undefined.

However, it is an instance of PFromJSVal, which means the following compilable code will crash

print . unpack . pFromJSVal $ nullRef
hamishmack commented 7 years ago

I think the same could be said of all the instances of pFromJSVal apart from the Maybe instance. When we use pFromJSVal we have to make sure the JSVal is valid and of the correct type. If it might be null we can use something like:

print . maybe "Value was null" unpack . pFromJSVal $ nullRef
louispan commented 7 years ago

I see. So the pFromJSVal is an 'unsafe' interface like unsafeCoerce. Maybe the comments could be updated to that effect?