Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #34214 > unrolled thread

Need to get the variable name from the called method

Started bysaransamurai7@gmail.com
First post2016-04-28 08:16 -0700
Last post2016-04-29 20:52 +0200
Articles 5 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Need to get the variable name from the called method saransamurai7@gmail.com - 2016-04-28 08:16 -0700
    Re: Need to get the variable name from the called method Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-04-28 13:31 -0400
    Re: Need to get the variable name from the called method Marcel Mueller <news.5.maazl@spamgourmet.org> - 2016-04-29 08:41 +0200
      Re: Need to get the variable name from the called method Lew <lewbloch@gmail.com> - 2016-04-29 11:18 -0700
        Re: Need to get the variable name from the called method Marcel Mueller <news.5.maazl@spamgourmet.org> - 2016-04-29 20:52 +0200

#34214 — Need to get the variable name from the called method

Fromsaransamurai7@gmail.com
Date2016-04-28 08:16 -0700
SubjectNeed to get the variable name from the called method
Message-ID<8735ea75-7793-49ad-a31d-ebf6c2691bac@googlegroups.com>
Code:

class Test {

public static void main(String args[]) {

int valueInt = 10;

function(valueInt);

}

void function(int param) {

// Here I need to get the output as "valueInt" & not 10.

}

Kindly help me to get the variable name "valueInt" to be printed inside the method 'function'

[toc] | [next] | [standalone]


#34216

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2016-04-28 13:31 -0400
Message-ID<nfth7u$vqo$1@dont-email.me>
In reply to#34214
On 4/28/2016 11:16 AM, saransamurai7@gmail.com wrote:
> Code:
>
> class Test {
>
> public static void main(String args[]) {
>
> int valueInt = 10;
>
> function(valueInt);
>
> }
>
> void function(int param) {
>
> // Here I need to get the output as "valueInt" & not 10.
>
> }
>
> Kindly help me to get the variable name "valueInt" to be printed inside the method 'function'

     Why?

     With a *lot* of *hard* work you *might* be able to discover the
string "valueInt" -- you'd probably need to find the caller (which
might be in a method of some completely different class), disassemble
that class' bytecodes, work your way backwards to the .class file from
which they came (if one exists), and hope there's enough ancillary
debugging information lying around in the .class file, and then ...

     ... of course, all this may come to naught if there is *no* name
corresponding to the argument, as in

	function(valueInt ^ 42);

     So, again: Why do you want to do this?  Are you hoping to get

	function(valueInt);
	function(otherInt);
	function(intangible);

to print something like

	valueInt = 10
	otherInt = 42
	intangible = -99193

?  What's your goal?

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [next] | [standalone]


#34220

FromMarcel Mueller <news.5.maazl@spamgourmet.org>
Date2016-04-29 08:41 +0200
Message-ID<nfuvms$ii5$1@gwaiyur.mb-net.net>
In reply to#34214
On 28.04.16 17.16, saransamurai7@gmail.com wrote:
> class Test {
>
> public static void main(String args[]) {
>
> int valueInt = 10;
>
> function(valueInt);
>
> }
>
> void function(int param) {
>
> // Here I need to get the output as "valueInt" & not 10.
>
> }
>
> Kindly help me to get the variable name "valueInt" to be printed inside the method 'function'

First of all a C++ executable has no means of variable names. They are 
just memory locations at this time. No more no less. So your entire 
program does no longer know how your variables were called. The 
information is lost during compilation.

You need to copy the variable name into your code as string literal. 
Basically by writing function("valueInt");
Now it will no longer compile because the types do no longer match.
Of course, this is error prone because the compiler can no longer check 
whether "valueInt" is a variable name or not.

Secondly if you want function() to print a string rather than an integer 
it need to take a string as argument. I.e. change the definition to
   void function(std::string param) {
This will resolve the type mismatch above.

C++ has no direct access to variable names. But the old C preprocessor 
that is still part of the language can do something similar.
If you define a helper macro like
   #define function_helper(x) function(#x)
Now you can write
   function_helper(valueInt);
to get the desired string.

Since the preprocessor as the name suggests runs before the compiler it 
will just transform your code into
   function("valueInt");
So there is no more type checking as if you wrote the double quotes 
yourself. So
   function_helper(smörebröd);
will compile as well.

However, if you need both, the name and the value, the situation changes.
Now your function is defined as
   void function(int value, std::string name) {
and your helper could be
   #define function_helper(x) function((x), #x)
Since the value of x is now used it must evaluate the an integer. The 
compiler will complain if you pass trash to function_helper because the 
resulting call
   function(valueInt, "valueInt");
need to be valid code.

But note what happens if you write.
   function_helper(valueInt + 1);
It will result in
   function(valueInt + 1, "valueInt + 1");
which is still valid code but maybe not what you intended.


Marcel

[toc] | [prev] | [next] | [standalone]


#34223

FromLew <lewbloch@gmail.com>
Date2016-04-29 11:18 -0700
Message-ID<a9d20fb7-2236-4a19-a164-b7e7347bde5f@googlegroups.com>
In reply to#34220
On Thursday, April 28, 2016 at 11:41:42 PM UTC-7, Marcel Mueller wrote:
> First of all a C++ executable has no means of variable names. They are 

C++?

> Secondly if you want function() to print a string rather than an integer 
> it need to take a string as argument. I.e. change the definition to
>    void function(std::string param) {

std::?
string?

> This will resolve the type mismatch above.
> 
> C++ has no direct access to variable names. But the old C preprocessor

C preprocessor?
 
> that is still part of the language can do something similar.
> If you define a helper macro like

macro?

>    #define function_helper(x) function(#x)

#define?
paren-paren?
#x?

Methinks these ideas will not work in Java.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#34224

FromMarcel Mueller <news.5.maazl@spamgourmet.org>
Date2016-04-29 20:52 +0200
Message-ID<ng0agp$pe6$2@gwaiyur.mb-net.net>
In reply to#34223
On 29.04.16 20.18, Lew wrote:
> On Thursday, April 28, 2016 at 11:41:42 PM UTC-7, Marcel Mueller wrote:
>> First of all a C++ executable has no means of variable names. They are
>
> C++?

Forget about it. I had two messages open in parallel and mixed up the 
groups.


Marcel

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web