this post was submitted on 28 Nov 2023
587 points (97.4% liked)

Programmer Humor

40805 readers
185 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 6 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] Mozingo@lemmy.world 38 points 2 years ago (3 children)

This sure looks like C#. I use typeof every once in a while when I want to check that the type of a reference is a specific type and not a parent or derived type. But yea, really not that often.

[–] PetDinosaurs@lemmy.world 9 points 2 years ago (4 children)

It looks exactly like c++ and c# and java and probably others.

[–] morhp@lemmy.wtf 17 points 2 years ago

Java only has instanceof and getClass, not typeof.

[–] Mozingo@lemmy.world 11 points 2 years ago (1 children)

But neither c++ or Java have typeof

[–] FooBarrington@lemmy.world 2 points 2 years ago

Typescript! Though it's less useful, since the Typescript types aren't available at runtime, so you'll just get object for non-primitive values.

[–] LapidistCubed@lemmy.world 1 points 2 years ago

Probably because Java and C# take much inspiration from C++. They aren't called "C-based" languages for nothing 😉

[–] r00ty@kbin.life 3 points 2 years ago

Yeah in C# it has quite a few uses.

I'm working on a background fun project where there's a base class that is for olde style CPU emulation. Where you can derive a class from the base class and essentially design 8bit style CPUs.

I have a separate class as a generic Assembler that will work with any of the created CPUs. But, to be able to do that I need to be able to get information about instructions, arguments, opcodes, registers etc from the derived class.

So the assembler is instantiated with Assembler<CPUType> and then it uses typeof to instantiate the actual CPU class being used to get all the information.

So, that's just an example of when you'd use something like this.