Kotlin reflection/dynamic function call in multi-platform environmentMoshi's Custom Adapter with RxAndroid & Retrofit & KotlinHow to get enum value of raw type from an enum class and a string in kotlinKotlin reflection error when calling functioninvoke() on out-projected Function objectKotlin - Multi platform featurekotlin : cannot parse string to enum with JacksonKotlin - specify type explicitly in IntelliJ after extracting a variable shortcutKotlin: enums with associated values; function inside 'enum entry' gets `unresolved reference` errorKotlin ReflectionHow to safely cast a reflected class in Kotlin
My colleague treats me like he's my boss, yet we're on the same level
Necessity of tenure for lifetime academic research
Moscow SVO airport, how to avoid scam taxis without pre-booking?
Properly unlinking hard links
Using font to highlight a god's speech in dialogue
What caused the end of cybernetic implants?
how can reincarnation magic be limited to prevent overuse?
How can I improve my formal definitions?
Why do motor drives have multiple bus capacitors of small value capacitance instead of a single bus capacitor of large value?
Ways you can end up paying interest on a credit card if you pay the full amount back in due time
What are ways to record who took the pictures if a camera is used by multiple people?
Padding a column of lists
How can I store milk for long periods of time?
Can a system of three stars exist?
What is the motivation behind designing a control stick that does not move?
Could a complex system of reaction wheels be used to propel a spacecraft?
An idiom for “Until you punish the offender, they will not give up offenses”
Don't look at what I did there
How smart contract transactions work?
awk print conditions
Heuristic argument for the Riemann Hypothesis
How were US credit cards verified in-store in the 1980's?
Can I leave a large suitcase at TPE during a 4-hour layover, and pick it up 4.5 days later when I come back to TPE on my way to Taipei downtown?
Should a TA point out a professor's mistake while attending their lecture?
Kotlin reflection/dynamic function call in multi-platform environment
Moshi's Custom Adapter with RxAndroid & Retrofit & KotlinHow to get enum value of raw type from an enum class and a string in kotlinKotlin reflection error when calling functioninvoke() on out-projected Function objectKotlin - Multi platform featurekotlin : cannot parse string to enum with JacksonKotlin - specify type explicitly in IntelliJ after extracting a variable shortcutKotlin: enums with associated values; function inside 'enum entry' gets `unresolved reference` errorKotlin ReflectionHow to safely cast a reflected class in Kotlin
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Given
class X
fun f() ...
fun g() ...
...
I would like to call f, g, or other class function by name, which is known dynamically at runtime. This is possible with kotlin-reflection, e.g.
val callback : KCallable<*>? = X::class.members.firstOrNull it.name == predVar
if (callback != null) {
callback.call(obj, ...)
The only snag here is that my multiplatform application has targets other than JVM. For instance, is kotlin reflection doable in javascript? Certainly, JS is dynamically typed language, thus my question is if it is already implemented there.
Alternatively, I can have a collection, or map of those functions. What would be the most elegant syntax of defining a list of functions, and having them callable dynamically? For example, given several enum type attributes, I can make a bunch of Boolean methods like this
var booleanDerivatives = hashMapOf<String, () -> Boolean>(
"breaksAfterComma" to fun (): Boolean
return options[breaksComma] === Breaks.After
,"breaksBeforeComma" to fun (): Boolean
return options[breaksComma] === Breaks.Before
,"breaksAfterLogicalConjunction" to fun (): Boolean
return options[breaksAroundLogicalConjunctions] === Breaks.After
)
However, unlike reflection, I seem to have to use string literals, because without reflection there is no mechanism translating a name into a string. Or there is?
kotlin
add a comment |
Given
class X
fun f() ...
fun g() ...
...
I would like to call f, g, or other class function by name, which is known dynamically at runtime. This is possible with kotlin-reflection, e.g.
val callback : KCallable<*>? = X::class.members.firstOrNull it.name == predVar
if (callback != null) {
callback.call(obj, ...)
The only snag here is that my multiplatform application has targets other than JVM. For instance, is kotlin reflection doable in javascript? Certainly, JS is dynamically typed language, thus my question is if it is already implemented there.
Alternatively, I can have a collection, or map of those functions. What would be the most elegant syntax of defining a list of functions, and having them callable dynamically? For example, given several enum type attributes, I can make a bunch of Boolean methods like this
var booleanDerivatives = hashMapOf<String, () -> Boolean>(
"breaksAfterComma" to fun (): Boolean
return options[breaksComma] === Breaks.After
,"breaksBeforeComma" to fun (): Boolean
return options[breaksComma] === Breaks.Before
,"breaksAfterLogicalConjunction" to fun (): Boolean
return options[breaksAroundLogicalConjunctions] === Breaks.After
)
However, unlike reflection, I seem to have to use string literals, because without reflection there is no mechanism translating a name into a string. Or there is?
kotlin
1
Wouldn'texpect/actual
keywords help in your case?
– yegodm
Mar 28 at 19:51
There are two parts of my question, and while Paul answered the first one, the second part is open. I assume your expect/actual hint relates to the first part; that is rolling out DIY implementation of reflection which is standard kotlin reflection in JVM env, and reflection proxy in JS via some sort of dynamic calls. Anyway, I have decided to follow the second venue outlined in my post and create a map of functions, as in the second code snippet. My minor gripe about this solution is that the "names" of the methods are string literals, which is probably not big deal at all.
– Tegiri Nenashi
Mar 29 at 0:49
add a comment |
Given
class X
fun f() ...
fun g() ...
...
I would like to call f, g, or other class function by name, which is known dynamically at runtime. This is possible with kotlin-reflection, e.g.
val callback : KCallable<*>? = X::class.members.firstOrNull it.name == predVar
if (callback != null) {
callback.call(obj, ...)
The only snag here is that my multiplatform application has targets other than JVM. For instance, is kotlin reflection doable in javascript? Certainly, JS is dynamically typed language, thus my question is if it is already implemented there.
Alternatively, I can have a collection, or map of those functions. What would be the most elegant syntax of defining a list of functions, and having them callable dynamically? For example, given several enum type attributes, I can make a bunch of Boolean methods like this
var booleanDerivatives = hashMapOf<String, () -> Boolean>(
"breaksAfterComma" to fun (): Boolean
return options[breaksComma] === Breaks.After
,"breaksBeforeComma" to fun (): Boolean
return options[breaksComma] === Breaks.Before
,"breaksAfterLogicalConjunction" to fun (): Boolean
return options[breaksAroundLogicalConjunctions] === Breaks.After
)
However, unlike reflection, I seem to have to use string literals, because without reflection there is no mechanism translating a name into a string. Or there is?
kotlin
Given
class X
fun f() ...
fun g() ...
...
I would like to call f, g, or other class function by name, which is known dynamically at runtime. This is possible with kotlin-reflection, e.g.
val callback : KCallable<*>? = X::class.members.firstOrNull it.name == predVar
if (callback != null) {
callback.call(obj, ...)
The only snag here is that my multiplatform application has targets other than JVM. For instance, is kotlin reflection doable in javascript? Certainly, JS is dynamically typed language, thus my question is if it is already implemented there.
Alternatively, I can have a collection, or map of those functions. What would be the most elegant syntax of defining a list of functions, and having them callable dynamically? For example, given several enum type attributes, I can make a bunch of Boolean methods like this
var booleanDerivatives = hashMapOf<String, () -> Boolean>(
"breaksAfterComma" to fun (): Boolean
return options[breaksComma] === Breaks.After
,"breaksBeforeComma" to fun (): Boolean
return options[breaksComma] === Breaks.Before
,"breaksAfterLogicalConjunction" to fun (): Boolean
return options[breaksAroundLogicalConjunctions] === Breaks.After
)
However, unlike reflection, I seem to have to use string literals, because without reflection there is no mechanism translating a name into a string. Or there is?
kotlin
kotlin
edited Mar 28 at 19:22
Tegiri Nenashi
asked Mar 27 at 23:55
Tegiri NenashiTegiri Nenashi
2,7062 gold badges14 silver badges18 bronze badges
2,7062 gold badges14 silver badges18 bronze badges
1
Wouldn'texpect/actual
keywords help in your case?
– yegodm
Mar 28 at 19:51
There are two parts of my question, and while Paul answered the first one, the second part is open. I assume your expect/actual hint relates to the first part; that is rolling out DIY implementation of reflection which is standard kotlin reflection in JVM env, and reflection proxy in JS via some sort of dynamic calls. Anyway, I have decided to follow the second venue outlined in my post and create a map of functions, as in the second code snippet. My minor gripe about this solution is that the "names" of the methods are string literals, which is probably not big deal at all.
– Tegiri Nenashi
Mar 29 at 0:49
add a comment |
1
Wouldn'texpect/actual
keywords help in your case?
– yegodm
Mar 28 at 19:51
There are two parts of my question, and while Paul answered the first one, the second part is open. I assume your expect/actual hint relates to the first part; that is rolling out DIY implementation of reflection which is standard kotlin reflection in JVM env, and reflection proxy in JS via some sort of dynamic calls. Anyway, I have decided to follow the second venue outlined in my post and create a map of functions, as in the second code snippet. My minor gripe about this solution is that the "names" of the methods are string literals, which is probably not big deal at all.
– Tegiri Nenashi
Mar 29 at 0:49
1
1
Wouldn't
expect/actual
keywords help in your case?– yegodm
Mar 28 at 19:51
Wouldn't
expect/actual
keywords help in your case?– yegodm
Mar 28 at 19:51
There are two parts of my question, and while Paul answered the first one, the second part is open. I assume your expect/actual hint relates to the first part; that is rolling out DIY implementation of reflection which is standard kotlin reflection in JVM env, and reflection proxy in JS via some sort of dynamic calls. Anyway, I have decided to follow the second venue outlined in my post and create a map of functions, as in the second code snippet. My minor gripe about this solution is that the "names" of the methods are string literals, which is probably not big deal at all.
– Tegiri Nenashi
Mar 29 at 0:49
There are two parts of my question, and while Paul answered the first one, the second part is open. I assume your expect/actual hint relates to the first part; that is rolling out DIY implementation of reflection which is standard kotlin reflection in JVM env, and reflection proxy in JS via some sort of dynamic calls. Anyway, I have decided to follow the second venue outlined in my post and create a map of functions, as in the second code snippet. My minor gripe about this solution is that the "names" of the methods are string literals, which is probably not big deal at all.
– Tegiri Nenashi
Mar 29 at 0:49
add a comment |
1 Answer
1
active
oldest
votes
According to the docs, no you can't access methods via reflection in the JS target. You would need to maintain your own collection of function references to achieve what you describe across both JVM and JS targets.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55388219%2fkotlin-reflection-dynamic-function-call-in-multi-platform-environment%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
According to the docs, no you can't access methods via reflection in the JS target. You would need to maintain your own collection of function references to achieve what you describe across both JVM and JS targets.
add a comment |
According to the docs, no you can't access methods via reflection in the JS target. You would need to maintain your own collection of function references to achieve what you describe across both JVM and JS targets.
add a comment |
According to the docs, no you can't access methods via reflection in the JS target. You would need to maintain your own collection of function references to achieve what you describe across both JVM and JS targets.
According to the docs, no you can't access methods via reflection in the JS target. You would need to maintain your own collection of function references to achieve what you describe across both JVM and JS targets.
answered Mar 28 at 0:19


Paul HicksPaul Hicks
9,9513 gold badges31 silver badges58 bronze badges
9,9513 gold badges31 silver badges58 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55388219%2fkotlin-reflection-dynamic-function-call-in-multi-platform-environment%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Wouldn't
expect/actual
keywords help in your case?– yegodm
Mar 28 at 19:51
There are two parts of my question, and while Paul answered the first one, the second part is open. I assume your expect/actual hint relates to the first part; that is rolling out DIY implementation of reflection which is standard kotlin reflection in JVM env, and reflection proxy in JS via some sort of dynamic calls. Anyway, I have decided to follow the second venue outlined in my post and create a map of functions, as in the second code snippet. My minor gripe about this solution is that the "names" of the methods are string literals, which is probably not big deal at all.
– Tegiri Nenashi
Mar 29 at 0:49