Issue2558
Created on 2017-02-28.19:57:55 by jamesmudd, last changed 2018-03-12.20:22:00 by jeff.allen.
msg11142 (view) |
Author: James Mudd (jamesmudd) |
Date: 2017-02-28.19:57:54 |
|
This is not a bug, but i wasn't sure where to put improvements? It would be nice if when calling a java method which takes a enum automatic type conversion from string could be done if possible. This would make calling enum methods more convenient.
At the moment a call to a method taking a enum with a string fails, even if the enum valueOf(String) method would work e.g
Jython 2.7.1b3 (, Feb 28 2017, 19:53:14)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_121
Type "help", "copyright", "credits" or "license" for more information.
>>> import java.time.LocalDate
>>> date = java.time.LocalDate.of(2015, 'DECEMBER', 3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: of(): 2nd arg can't be coerced to int, java.time.Month
>>>
Since Jython already does other similar type coercion I think this would be a nice extension, but maybe i'm missing a reason why its a really bad idea?
|
msg11143 (view) |
Author: James Mudd (jamesmudd) |
Date: 2017-02-28.20:02:04 |
|
I have a pull request which can enable this https://github.com/jythontools/jython/pull/58
After this change the behaviour is
Jython 2.7.1b3 (, Feb 28 2017, 19:05:08)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_121
Type "help", "copyright", "credits" or "license" for more information.
>>> import java.time.LocalDate
>>> date = java.time.LocalDate.of(2015, 'DECEMBER', 3)
>>> date
2015-12-03
>>> date.getMonth().__class__
<type 'java.time.Month'>
>>>
Where 'DECEMBER' is automatically coerced into java.time.Month.DECEMBER
I would be interested in what people think of this idea?
|
msg11160 (view) |
Author: Stefan Richthofer (stefan.richthofer) |
Date: 2017-03-03.11:13:08 |
|
I am actually much for improving Java integration, but IMO Strings and Enums are quite not the same (and don't even share an is-relation in any direction). Looking at https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html an Enum can also be a Planet etc, have custom properties, even methods.
Note that enumMethod in your test also wouldn't accept a String when called in Java-code.
You suggest to let Jython convert silently a String in the role of an element name into a value of Enum subtype. For me this looks like mixing up something like a lookup (e.g. in a map) with an is-relation. Or stated differently: Too much magic, even for Jython users.
That said, I would like to have a proper way to allow the method call you mention (maybe there already is?)
E.g. something like an explicit (is better than implicit...^^) way to let Jython perform Enum-lookup from Python-code.
Maybe we could have an integration with the official Python module for enums https://pypi.python.org/pypi/enum34 (for Python 3, but also backported to several Python 2 versions).
I will think about it and would also wish to hear more opinions.
|
msg11783 (view) |
Author: Jeff Allen (jeff.allen) |
Date: 2018-03-12.08:49:15 |
|
I think automatic coercion of a string would turn out badly: we'll discover cases where two signatures match. Compare the insoluble #1781.
We can do this:
>>> import java.time.LocalDate
>>> from java.time import Month
>>> java.time.LocalDate.of(2015, Month.DECEMBER, 3)
2015-12-03
or:
>>> from java.time.Month import *
>>> DECEMBER
DECEMBER
>>> java.time.LocalDate.of(2015, DECEMBER, 3)
2015-12-03
Does that not do the trick?
|
msg11784 (view) |
Author: James Mudd (jamesmudd) |
Date: 2018-03-12.12:49:23 |
|
Yes there are lots of better solutions than what is proposed here which is almost certainly a bad idea.
This should be closed as won't implement.
|
msg11785 (view) |
Author: Jeff Allen (jeff.allen) |
Date: 2018-03-12.20:22:00 |
|
Thanks James. Looking at GitHub, I see you had second thoughts (and third) nearly a year ago.
|
|
Date |
User |
Action |
Args |
2018-03-12 20:22:00 | jeff.allen | set | status: open -> closed resolution: remind -> rejected messages:
+ msg11785 |
2018-03-12 12:49:23 | jamesmudd | set | messages:
+ msg11784 |
2018-03-12 08:49:16 | jeff.allen | set | keywords:
+ RFE type: behaviour -> rfe messages:
+ msg11783 nosy:
+ jeff.allen |
2017-03-03 11:15:55 | stefan.richthofer | set | priority: low resolution: remind title: Automatic string to enum coercion -> Feature request: Automatic string to enum coercion |
2017-03-03 11:13:10 | stefan.richthofer | set | nosy:
+ stefan.richthofer messages:
+ msg11160 |
2017-02-28 20:02:04 | jamesmudd | set | messages:
+ msg11143 |
2017-02-28 19:57:55 | jamesmudd | create | |
|