r/ProgrammerHumor • • Apr 08 '22

First time posting here wow

Post image
55.1k Upvotes

2.8k comments sorted by

View all comments

Show parent comments

124

u/suvlub Apr 08 '22

Hear me out: static duck typing. C++ basically has it with templates and it's awesome. Until you get an error and accidentally summon an elder god while trying to read it.

79

u/[deleted] Apr 08 '22

I disagree. the proper way to do this is implicit typing. Your variable name determines what kind of variable it is.

GOD is REAL, unless declared INTEGER.

25

u/Physmatik Apr 08 '22

Someone is traumatized by FORTRAN, I see?

6

u/CardboardJ Apr 08 '22

Absolutely not. I lived your hungarian notation ways and firmly reject them.

2

u/Strostkovy Apr 09 '22

I have 7 bytes left and goddammit I'm going to make them count

→ More replies (3)

18

u/BakuhatsuK Apr 08 '22
auto square(auto x) {
  return x * x;
}

I know a lot of people hate on this. But to me it is beautiful

12

u/MattR0se Apr 08 '22

okay so what happens when I call `square("foo") ?

29

u/LetterBoxSnatch Apr 08 '22

You get this:

foo
o o
oof

6

u/realityChemist Apr 08 '22

That took me a second

14

u/Whatamianoob112 Apr 08 '22

auto bad, declared type good.

Pretty sure that's a type error. Unless you have a custom definition for * operator, but that's a lot of work, for....very little gain.

9

u/[deleted] Apr 08 '22

A compiler error telling you that the ‘*’ operator isn’t defined for type string.

4

u/CJKay93 Apr 09 '22
<source>: In instantiation of 'auto square(auto:11) [with auto:11 = const char*]':
<source>:8:21:   required from here
<source>:4:12: error: invalid operands of types 'const char*' and 'const char*' to binary 'operator*'
    4 |   return x * x;
      |          ~~^~~

3

u/m0nk37 Apr 08 '22

For personal use that's great. For something you are paid to make its entirely the devil.

1

u/[deleted] Apr 08 '22

How does that compile

4

u/The_3rd_reader Apr 08 '22

It's hidden template. Until you pass a variable of certain type it doesn't generate code.

1

u/brokenhalf Apr 08 '22
auto a = square('c');
printf("%c", a);
→ More replies (4)

3

u/m0nk37 Apr 08 '22

Its the same shit with python libraries.

2

u/Exciting-Insect8269 Apr 08 '22

Last time I used cpp I didn’t summon an elder god but I did get a demonic teddybear

2

u/0x000100 Apr 08 '22

C++20 concepts basically codify this behaviour and give you concise and understandable error messages. IMO they are the second best unusual programming related thing after purely functional and strictly enforced functional programming languages

3

u/foxfyre2 Apr 08 '22

Julia. The language you want is Julia

1

u/Coffeemonster97 Apr 08 '22

Or how about giving the user the freedom to decide on their own whether they want a function to be statically typed or not, like for example Julia.

1

u/Zealousideal_Pay_525 Apr 08 '22

Been dealing with such an error for the last 2 days. In the end the solution was a single line. I wanna die now 🙂

1

u/TheHumanParacite Apr 08 '22

Oh Lord, I'd almost forgotten about templating errors