Ok, at some point we made a big mistake.

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

305 points · 31 comments · view on lemmy.world

31 Comments

superfes@lemmy.world · 58 pts · 2y

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.

GissaMittJobb@lemmy.ml · 52 pts · 2y (9 replies)

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.

tyler@programming.dev · 14 pts · 2y (8 replies)

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

ris@feddit.de · 19 pts · 2y (1 reply)

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

MinekPo1@lemmy.ml · 3 pts · 2y

note that it continues onto the next line

Meltrax@lemmy.world · 5 pts · 2y

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

peter@feddit.uk · 5 pts · 2y (4 replies)

For example?

sloppy_diffuser@sh.itjust.works · 4 pts · 2y (3 replies)

Not OP, and these examples are not unreadable, but they are a few steps up from your typical generics.

https://effect-ts.github.io/effect/effect/Unify.ts.html#unify

https://effect-ts.github.io/effect/effect/Pipeable.ts.html

marcos@lemmy.world · 8 pts · 2y (1 reply)

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.

sloppy_diffuser@sh.itjust.works · 3 pts · 2y

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@lemmy.world · 4 pts · 2y

Right to jail. Right now.

clericc@lemmy.world · 30 pts · 2y

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@lemmy.world · 29 pts · 2y (5 replies)

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

MinekPo1@lemmy.ml · 44 pts · 2y

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

::: spoiler 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@lemmy.world · 3 pts · 2y (3 replies)

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

mvirts@lemmy.world · 1 pts · 2y (2 replies)

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@lemmy.world · 4 pts · 2y (1 reply)

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@lemmy.world · 1 pts · 2y

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

ryannathans@aussie.zone · 23 pts · 2y

Why tf

algernon@lemmy.ml · 11 pts · 2y

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

9point6@lemmy.world · 10 pts · 2y (1 reply)

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@lemmy.world · 5 pts · 2y

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

baseless_discourse@mander.xyz · 10 pts · 2y

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

Aurenkin@sh.itjust.works · 9 pts · 2y (1 reply)

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.

settoloki@lemmy.one · 1 pts · 2y

I see what you did there

Thcdenton@lemmy.world · 7 pts · 2y

I don't want to look at this anymore

loaf@sh.itjust.works · 7 pts · 2y

My soul hurts

velox_vulnus@lemmy.ml · 5 pts · 2y
[ removed ]
toastal@lemmy.ml · 3 pts · 2y

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@lemmy.world · 3 pts · 2y (1 reply)

That's why macro_rules!

beeb@lemm.ee · 5 pts · 2y

This is not rust :D Nothing will save typescript

nayminlwin@lemmy.ml · 1 pts · 2y

Too much focus on compile time.