-
-
Notifications
You must be signed in to change notification settings - Fork 203
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
String prisms for primitives. #32 #33
Conversation
I think I figured it out. |
object string extends StringInstances | ||
|
||
trait StringInstances { | ||
def stringToBoolean: SimplePrism[String, Boolean] = SimplePrism(_.toString, s => Try(s.toBoolean).toOption) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Bounded, you can find a safeCast method that simplifies creation of that kind of Prism
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
safeCast doesn't help in this situation, because the string's Ordering and int's boundedness don't help us, as String isn't numeric.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could add a new function for constructing prisms catching exceptions.
Where should we put it?
Shold I rebase and make a new pull request to remove unnessesary stuff from history? |
What should we do about the identity failing for localized numbers? |
I really don't know where to put things now with these construction functions.
The exception construction could be maybe put in Prism's object for more intuitive construction for users. |
No you don't need to rebase. Good points about helper functions, let's put them in the companion object of Prism as you suggested. I will have a read about localised numbers |
Regarding localised numbers, we should probably make stringToFloat take a Locale and use http://stackoverflow.com/questions/888088/how-do-i-convert-a-string-to-double-in-java-using-a-specific-locale. |
@julien-truffaut I think it would be much cleaner to just parse only normal numbers. |
If there is a normal/standard string representation of float and double then let's use it. Otherwise, we can drop string prism for float and double for now. |
IMO we should keep current Java parsing behavior and possibly pass implicit Locale |
@13h3r Where should we supply the implicit Locale from? |
Another option is to split the string lenses to 2 groups: localized and unlocalized. |
@NightRa yes, you should import implicit, or import default locale somehow. This avoid us to push system wide parameter into every call |
Having to import implicits is a usage show stopper from personal experience. (ScalaFX lifting of functions to wrappers) |
@NightRa It seems that the problem you had is not related to Locale and floating point. I launched test for stringToFloat and DoubleToFloat pass witthout problem. However, Int and Long fails for an interesting reason: some non digits characters are translated to number when you call toInt / toLong on them, see http://www.fileformat.info/info/unicode/char/aa59/index.htm and http://www.alanwood.net/unicode/cham.html |
@julien-truffaut This is what I was talking about. |
Ah sorry I misunderstood. Then I think we should limit to basic digit numbers, we can always add more advance prisms later on |
@julien-truffaut What will we parse with then? |
We could roll out our own in part of the Int-Digit Isomorphism. #34 |
let's have a look around if it exists some parser that do the job. Otherwise yes, we can roll our own. |
Changing branch. |
I have added String prisms for primitives.
ScalaCheck gives out some peculiar counter examples which we have to examine.