#Type Parameter Issues

53 messages · Page 1 of 1 (latest)

high lodge
#

This one has a lot of code. (it was too much for discord, find em here https://pastebin.com/uXUCwmVQ)
The issue is in the construction of a request. I have no clue what is going on, but for some reason, I can do this with absolutely no errors:

System.out.println(Request.of(RType.INTEGER, new ArrayList<>(), 3, true));

And, yes, I tried this:

System.out.println(new Request<>(RType.INTEGER, new ID(""), true));

For some reason, I can set an integer to a boolean? I think I am a very advanced programmer, but this has me stumped. Any help appreciated!

dull sluiceBOT
#

This post has been reserved for your question.

Hey @high lodge! Please use /close or the Close Post button above when your problem is solved. 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.

manic kestrel
#

Well booleans are just intergers under the hood (0 being false and 1 being true).

#

Though not fully sure how requests work as I've never used them

#

Ah I see it's the pastebin you posted

#

Let me take a quick look

#

Okay so the first one works as it is using this function:

public static <H> Request<H> of(IRType<H> type, List<ID> activeIds, int idLength, H data) {```
#

What's confusing regarding it?

high lodge
#

Wdym

#

Well Boolean doesn't extend integer, so I have no clue why it's allowing me to do that

#

And same with other stuff like strings

manic kestrel
#

In computer science, the Boolean (sometimes shortened to Bool) is a data type that has one of two possible values (usually denoted true and false) which is intended to represent the two truth values of logic and Boolean algebra. It is named after George Boole, who first defined an algebraic system of logic in the mid 19th century. The Boolean da...

high lodge
#

Well that's not how it's worked for me in the past

#

And I'm doing Boolean no boolean I'm pretty sure

manic kestrel
#

If you are using the keywords true and false it will be using boolean

high lodge
#

Ok

#

But there is no string, only String, so I have no clue on that one

manic kestrel
#

System.out.println(Request.of(RType.STRING, new ArrayList<>(), 3, "string"));

#

Does this work?

manic kestrel
#

As a string under the hood is actually a char[]

next sparrow
#

RType implements IRType<Object>, therefore although elements in your enum use different generics, they all in fact use Object generic.
That's why your function with different types runs just fine, because your Integer and Boolean treated as Object, so no error here!

manic kestrel
#

The primitives only matter in the boolean -> int conversion, they have nothing to do with strings

next sparrow
#

I guess you could've avoided that, if you defined each RType value individually in a separated classes, implementing IRType<Integer>, IRType<Boolean> and such instead of blank IRType<Object>

#

But i dunno, I fall asleep

next sparrow
#
Request.of(RType.INTEGER, new ArrayList<>(), 3, true)

is the same as:

Request.of<Object>(RType.INTEGER, new ArrayList<>(), 3, true)
high lodge
#

Yeah. If I add a variable like
Request<Integer>
it recognizes it as an error. Is there a way to adjust the method to fix it? I don't want to have to state the class twice to get it to work.

next sparrow
high lodge
#

So how to fix that

next sparrow
#
public class RType<T> implements IRType<T> {
    public static final RType<String> STRING = new RType<>(String.class, s -> s + " :)");
    public static final RType<Boolean> BOOLEAN = new RType<>(Boolean.class, b -> !((Boolean) b));
    public static final RType<Integer> INTEGER = new RType<>(Integer.class, i -> (int) i + 5);
 
    private final Class<?> dataType;
    private final Function<Object, Object> processRequest;
 
    RType(Class<?> dataType, Function<Object, Object> processRequest) {
        this.dataType = dataType;
        this.processRequest = processRequest;
    }
 
    @Override
    public Class<Object> getDataType() {
        return (Class<Object>) dataType;
    }
 
    public String getName() {
        return super.toString();
    }
 
    @Override
    public Object processRequest(Object requestData) {
        return null;
    }
 
    @Override
    public String toString() {
        return "RType{dataType=" + dataType + ", name=" + getName() + "}";
    }
}
next sparrow
#

God, why is it so hard to type on phone, it just freezes every few seconds 😭

#

@high lodge

high lodge
#

Ye I'm on phone too

#

And that kinda defeats the purpose of the enum. If it's the hard then I guess I'll just use strings for identifiers.

next sparrow
#

Except it will work, and work beautifully with datatype checks from java compiler

high lodge
#

Cool

#

I meant because this is for server-client stuff, so it will need to be serializable, any ideas on a method that can get the variable name?

#

Actually better idea: integer id

next sparrow
high lodge
#

What is java reflection?

next sparrow
#

A bunch of classes and methods used to gain information about the structure of Java program itself. You already used one of its features: Class<T>

high lodge
#

oh

#

just asked chat gpt so I think I understand more

#

looks cool

#

thank you

dull sluiceBOT
# high lodge thank you

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.

next sparrow
#

and powerful

#

You're welcome