#Making an object that can be various types?

28 messages · Page 1 of 1 (latest)

deft pagoda
#

I have a byte[] and a short[] used for the same thing.
I want to switch from using the byte[] and short[] based on the maximum value needed, which changes occasionally.
My idea is to have an Object that is swapped between referencing the byte[] or the short[] as needed.
The part I can't figure out is casting the Object to the array type its referencing. I tried this to alter the contents of the array the object is referencing:

 ((array.getClass())(array))[pos] = ...

but it just says "the result of getClass() is unused" (aka it isnt trying to cast the Object with the result like i want it to)

brazen jacinthBOT
#

This post has been reserved for your question.

Hey @deft pagoda! 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 marked as dormant 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.

tough flicker
#

Why aren't you always using a short[]?

#

but no you can't cast like that

#

what you could do in principle is create wrapper classes for each type of array with a common (sealed) interface but I don't see much of a point in that tbh

deft pagoda
#

this is for chunks of data

#

with palette compression

#

so if the palette only has 1 or 2 values i can use an array of booleans to reference that palette

#

if it has 2-127 values i can use an array of bytes

#

then shorts for values above that

stable tendon
#

but it just says "the result of getClass() is unused" (aka it isnt trying to cast the Object with the result like i want it to)

#

is this an error

#

or a warning

deft pagoda
#

warning

stable tendon
#

o

deft pagoda
#

it also says this

#

if i hover over the red part

stable tendon
#

you could ig use an if statement

#

to know when to swap

deft pagoda
#

yeah i can i just wanted something cleaner and less annoying to make

#

plus it would be faster to not have to use those if statements

stable tendon
#

one if statement doesnt sound too bad tbh

#

so yea try that

deft pagoda
#

yeah that reduced memory usage from 1.54gb to 1.03gb

#

worth

#

and the savings from adding the boolean array might be even bigger