this post was submitted on 11 Feb 2024
305 points (98.4% liked)

Programmer Humor

32174 readers
992 users here now

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

Rules:

founded 5 years ago
MODERATORS
305
submitted 7 months ago* (last edited 7 months ago) by lawrence to c/[email protected]
 

Hacker News post about this: https://news.ycombinator.com/item?id=39309783 (source available)

top 32 comments
sorted by: hot top controversial new old
[–] superfes 58 points 7 months ago

I've written some magic templates that I assume are not easy to read by those who don't know.

But this is seemingly unmaintainable... terrifying... and kind of neat.

[–] [email protected] 52 points 7 months ago (1 children)

This doesn't actually read as serious TypeScript, moreso as someone trying to showcase unhinged code.

I'd be happy to be proven wrong with a link to the source code so that I can look the beast in the eye.

[–] [email protected] 14 points 7 months ago (3 children)

Take a look at some typescript libraries and frameworks and you will see stuff like this. Completely unreadable mess.

[–] [email protected] 19 points 7 months ago (1 children)

I have seen image recognition or text RPGs with type script types, but const ok:true = true as Grid< 4, 9, 2 Wtf

[–] [email protected] 3 points 7 months ago

note that it continues onto the next line

[–] Meltrax 5 points 7 months ago

Styled Components' type system is one of the most impressive and most fucked up things I've ever had to dive into.

[–] [email protected] 5 points 7 months ago (1 children)
[–] [email protected] 4 points 7 months ago (2 children)
[–] marcos 8 points 7 months ago (1 children)

Creating basic functionality for the language always leads to unreadable code.

The C++ version would be much, much worse, and the Lisp version is Lisp.

[–] [email protected] 3 points 7 months ago

Agree. What I linked provides core type support for that library. The pipe one is just a bunch of overloads to support a specific way of handling function composition to appease the TypeScript type checker.

There are a lot of typing hacks in that library to simulate higher kinded types.

[–] thesporkeffect 4 points 7 months ago

Right to jail. Right now.

[–] clericc 30 points 7 months ago* (last edited 7 months ago)

After 5 minutes of staring at it: Its typesystem sudoku. Each row and each col in the grid must add up to 15 (T<>), bit each number in the grid must be different (Df<>).

Grid will only be a type alias for the value true (google "Dependent types") only if all Type Parameters (wich are values) hold up to the Sudoku conditions).

The file would not compile with "true as Grid" when grid type-aliases to false.

Fun to understand.

EDIT: too late

[–] thesporkeffect 29 points 7 months ago (2 children)

I like to think I can usually look at code in languages I don't know and still get the gist of what it does but I am drawing a complete blank. Is this even slightly legible to anyone and if yes please explain

[–] [email protected] 44 points 7 months ago

TL;DR: Grid simplifies to true, if and only if it is a 3x3 magic square.

full explanation

  • Fifteen is an array of length 15
  • T checks if an array of length A+B+C is equivalent to an array of length 15, thus checking if A+B+C is equal to 15
  • And is simplifies to X if A is true, else it simplifies to false
  • Df checks if A and B are Diffrent , simplifying to X if they are
  • Grid first checks if every row, column and diagonal is equal to 15, then checks if every item is unique.
[–] SloppyPuppy 3 points 7 months ago* (last edited 7 months ago) (1 children)

Yea, i was really trying to understand… and then I was like yeah fuck this and regex too.

[–] mvirts 1 points 7 months ago* (last edited 7 months ago) (1 children)

Gotta learn regex... C++ not so much 😹

Fear not, template abuse is not a necessary practice

Edit: wait what is this I thought it was c++ at first. Java? C#? Has an extends keyword

[–] immutabletest 4 points 7 months ago* (last edited 7 months ago) (1 children)

This is a Typescript type-level computation (which is Turing-complete AFAIK). However this kind of trickery is usually done for fun and to explore how much could be achieved just on the compile step.

[–] mvirts 1 points 7 months ago

Lol thanks that saves me a lot of googling 😹 that makes a lot of sense

[–] [email protected] 23 points 7 months ago
[–] [email protected] 11 points 7 months ago

I think I can pinpoint the exact date things went sideways. It was a dark day on Monday, October 1, 2012.

[–] 9point6 10 points 7 months ago* (last edited 7 months ago) (1 children)

I'm really trying to figure out what this is used for and why it was done this way.

I'm not having much success

[–] platypus_plumba 5 points 7 months ago

Looks like something that checks that the rows in a grid att up to 15. Why? IDK, a game?

[–] [email protected] 10 points 7 months ago* (last edited 7 months ago)

Yeah, and apparently type checking/inference is trivial, says the "CTO" of Xitter. /s

[–] [email protected] 9 points 7 months ago (1 children)

This seems like a generic type of problem that could happen to anyone. Hopefully we can learn from this and avoid appending it to our already large grid of problems.

[–] [email protected] 1 points 7 months ago

I see what you did there

[–] Thcdenton 7 points 7 months ago

I don't want to look at this anymore

[–] [email protected] 7 points 7 months ago

My soul hurts

[–] [email protected] 5 points 7 months ago* (last edited 7 months ago)

Show that to your niece and nephew and you'll be a certified Haxxor™

[–] [email protected] 3 points 7 months ago

If TypeScript didn’t have terrible type-level ergonomics, this wouldn’t look so bad—even if this toy example is largely just a brain exercise

[–] RustyNova 3 points 7 months ago (1 children)
[–] [email protected] 5 points 7 months ago

This is not rust :D Nothing will save typescript

[–] [email protected] 1 points 7 months ago

Too much focus on compile time.