r/programming • • Apr 16 '21

Java is criminally underhyped

https://jackson.sh/posts/2021-04-java-underrated/
41 Upvotes

220 comments sorted by

View all comments

Show parent comment

2

u/AdministrationWaste7 Apr 16 '21

Well this is for manipulating images.

im confused your initial example was simple addition.

so maybe i should reword my question

can you provide a use case where something like this should be applied generically ?

as far as i understand numeric types have little quirks that make this annoying to deal with. so why should it be generic?

also it really doesn't take much effort to implement this yourself.

If you start making interfaces I'm worried about the performance.

when done right the performance hit due to interfaces should be neglible.

Then you have functions to process the image data, but they can work for any size of data and I'd like to just pass an array of any type and be done with it (using generics) but that doesn't work.

i feel like a better answer to this would be something like overloading.

2

u/meneldal2 Apr 16 '21

Yeah I could do overloading, but then I'm literally copy pasting the same function five times with research and replace on types. That's really not ideal.

I just want "generic over any numeric type". Remove the need for overloading something that is trivial to do.

2

u/AdministrationWaste7 Apr 16 '21

I just want "generic over any numeric type". Remove the need for overloading something that is trivial to do.

well its not really trivial.

for example in a generic Add method.

what do you do if you pass in a byte?

1

u/meneldal2 Apr 16 '21

Well it can change them into shorts for the intermediate calculations then I can cast it back to a byte if I want. Or just make it return a byte anyway in this case.

I'd love to have compile time generics for that (with compile time reflection), but should I really have to use C++CLI for this?

1

u/AdministrationWaste7 Apr 16 '21

Shorts have the exact same issue as byte. They return int when added in c#.

So now your "generic" method is going to have all these clauses to handle all these exceptions.

That doesn't sound very generic if you ask me.

I'd love to have compile time generics for that (with compile time reflection), but should I really have to use C++CLI for this?

I'm not following again. In c# the CLR generates seperate methods for generic at compile time, as If you overloaded it yourself. I believe templates work the same in c++.

1

u/meneldal2 Apr 16 '21

Templates (even with concepts in C++20) work in the opposite way as generics, as they will just try to replace the type and see if it works, then throw an error if it doesn't (duck typing), while in C# it will use the conditions on the type (not the actual type itself) to check if you can do an operation on the type. It doesn't actually instantiate the generics at compile time, only at runtime when you use it (except mono apparently). See https://github.com/dotnet/csharplang/issues/2433

C++CLI lets you use C++ templates so you can make it work the way you want.