Skip to content

Commit

Permalink
Issue 374 - Extend docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafal Mucha committed Mar 9, 2023
1 parent ba0caad commit 0471bbb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions py4j-python/src/py4j/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
PYTHON_PROXY_TYPE = "f"

class TypeHint:
"""Enables users to provide a hint to the Python to Java converter specifying the accurate data type for a given value.
Essential to enforce i.e. correct number type, like Long."""
def __init__(self, value, java_type):
self.value = value
self.java_type = java_type
Expand Down
21 changes: 21 additions & 0 deletions py4j-web/advanced_topics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,27 @@ Java methods slightly less efficient because in the worst case, Py4J needs to
go through all registered converters for all parameters. This is why automatic
conversion is disabled by default.

.. _explicit_conversion:

Explicit converting Python objects to Java primitives
-----------------------------------------------------

Sometimes, especially when ``auto_convert=True`` it is difficult to enforce correct type
passed from Python to Java. Then, ``TypeHint`` from ``py4j.protocol`` may be used.
``java_type`` argument of constructor should be one of Java types defined in ``py4j.protocol``.

So if you have method in Java like:

.. code-block:: java
void method(HashSet<Long> longs) {}
Then you can pass arguments with correct type to this method with ``TypeHint``

::

>>> set_with_longs = { TypeHint(1, LONG_TYPE), TypeHint(2, LONG_TYPE) }
>>> gateway.jvm.my.Class().method(set_with_longs)

.. _py4j_exceptions:

Expand Down

0 comments on commit 0471bbb

Please sign in to comment.