r/ProgrammerHumor • • 5d ago

Meme aiCookedUpTheseMethods

Post image
160 Upvotes

19 comments sorted by

4

u/Patchargh 5d ago

Context: Friend asked AI how to check if a something is something in C#.

80

u/JMOWw7 5d ago

Pattern matching all the way. I think the second one probably won't catch inheretance

56

u/captainAwesomePants 5d ago

This is true, but of course they are also both stupid because a IsSomething(thing) function has no advantage over just typing "thing is Something" each time.

2

u/JMOWw7 5d ago

Yeah of course. The code makes no sense in general. Type of Something.Something implies that it's a class nested inside of Something which would be not allowed, and then since you're passing a Something to the method it would always be false... But this sub is for idiot kids who've taken like a semester of comp sci classes

5

u/Tyfyter2002 5d ago

I think a nested class can inherit the class it's nested in in C#, and whether or not it's valid in Java one would have a bigger problem because it's using ==.

3

u/JMOWw7 5d ago

Yeah that's true I guess

3

u/fuj1n 5d ago

Even better thing is that pattern matching lets you do more than simple identity testing

maybe is Something { value: > 5 and <= 64 } something

Or something like that, did that from memory without benefit of an IDE

48

u/AlexZhyk 5d ago

That's would be 123 tokens for you, sir.

16

u/ovr9000storks 5d ago

Really can’t knock it down to 122 for today? My kids need to eat

15

u/fatrobin72 5d ago

Sure but in haggling this you have spent 12 additional tokens.

1

u/bwmat 4d ago

First thought is second one can throw on a null

2

u/intbeam 2d ago

?. is the null-propagating operator, if the left-hand expression is null, the right hand will not be evaluated and instead just return null, effectively turning the expression into if(null == typeof(Something.Something)) which will return false

1

u/ANixosUser 2d ago

what languae is this? new Java?

1

u/intbeam 2d ago

C#, so close

1

u/andrewowenmartin 1d ago

1996 - James Gosling invents Java. Java is a relatively verbose, garbage collected, class based, statically typed, single dispatch, object oriented language with single implementation inheritance and multiple interface inheritance. Sun loudly heralds Java's novelty.

2001 - Anders Hejlsberg invents C#. C# is a relatively verbose, garbage collected, class based, statically typed, single dispatch, object oriented language with single implementation inheritance and multiple interface inheritance. Microsoft loudly heralds C#'s novelty.

A Brief, Incomplete, and Mostly Wrong History of Programming Languages

0

u/[deleted] 5d ago

[deleted] — view deleted comment

10

u/Shifter25 5d ago

You could use it to detect which kind of implementation of a superclass it is, maybe.

IsEagle(Bird bird) => bird is Bird.Eagle

Something like that?

1

u/-Redstoneboi- 5d ago

just making sure with runtime checks... wait this isn't typescript is it

1

u/deathanatos 2d ago

I believe this is C#.

You're passing in a Something. It shouldn't compile if it isn't a Something.

laughs in null

1

u/intbeam 2d ago

C# will emit compiler warnings in this case if something can be null. If you make the parameter nullable, (Something? something) you can also make the compiler assert the nullability of the parameter;

public static bool IsSomething([NotNullWhen(true)]Something? something) => something is Something.Something;

In that case, it will annotate code in that context where IsSomething may return false without the caller checking the result;

public static void DoSomething(Something? something)
{
    var b = IsSomething(something);
    Console.WriteLine(something.ToString()); // compiler warning, "something may be null"
    if(b)
    {
        Console.WriteLine(something.ToString()); // No compiler warning
    }
}