#Accessing primitives as references

15 messages · Page 1 of 1 (latest)

reef maple
#

I've got a bit of a weird problem. I have some primitives (doubles, integers, booleans) that I need to access via reference. I'm making an automatic config system, and it's designed to work with just one function per parameter, addConfigParameter(key, variable), and anytime the reload function is called it should change variable based on the config file. This is possible if I use a reference type, but I'd like it to work for any code so hard coding configurable classes is a no-no. Anyone know of a way I can do this? I tried using the primitive wrapper classes Double vs double but it didn't seem to help.

vagrant narwhalBOT
#

This post has been reserved for your question.

Hey @reef maple! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

lament prism
#

Double should have worked what the problem you are facing? any examples?

river ermine
#

Wrappers are immutable, so that's why they don't work too

reef maple
#

Yea, this is the code I create the parameter with

Double val = 1.0;
AutoConfig conf = new AutoConfig();
conf.addConfigValue("testconf", val);```

Then I set it with 
```java
private void setValue(Object obj, String key, YamlMapping map){
        if(obj instanceof Double){
            obj = map.doubleNumber(key);
        }```

which I've confirmed runs before I log the value, and map.doubleNumber(key) returns 42, but when I log val it's stil 1.0
#

I guess I could just create a class that holds a value and use that so I can get a reference to it but I feel like theres a better way than creating a class for every type of parameter I might use

river ermine
#

basically, that's bad approach, since java arguments are passed as values always. Consider refactoring your code in a way, that method returns object, and then you assign it to a neened variable.

river ermine
#

like, ```java
class MyWrapper<T> {
T element;
public void setElement(T el) {
element = el;
}

public T getElement() {
return element;
}
}```

reef maple
#

Alright, tysm

#

I'll give it a go

river ermine
#

you are welcome

reef maple
#

That seems to work how I want, thanks again!

vagrant narwhalBOT
# reef maple That seems to work how I want, thanks again!

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.