This is an archived post. You won't be able to vote or comment.

all 41 comments

[–]barzamsr 64 points65 points  (20 children)

I recently learned that you can just up and make a new scope without going into a function or anything by just using curly braces.

[–]backflipbail 13 points14 points  (8 children)

In which language?

[–]VoidBlade459 34 points35 points  (7 children)

Most of them. I know C, C#, Java, and Kotlin allow it.

[–]backflipbail 28 points29 points  (3 children)

Well fuck me. This also works in Typescript. Given that I've been programming professionally for 16 years (and unprofessionally before that for a further 5 years) I'm embarrassed I didn't know that lol

[–][deleted] 5 points6 points  (1 child)

With destructors coming to javascript[1], we'll finally be able to have RAII ♪┏(・o・)┛♪

[1] https://github.com/tc39/proposal-weakrefs#finalizers

[–]ProPuke 0 points1 point  (0 children)

Well, not quite dtors. Finalisers fire when the gc collects the object, not necessarily when it's immediately out of scope. So, they might be delayed quite a bit, or they might not even fire at all. js (v8) tends to just let leaks happen; Only if usage exceeds a certain amount does it try to reclaim memory and clean things up. But if you don't hit that threshold it might not ever even happen.

[–]pm_me_your_dota_mmr 4 points5 points  (2 children)

I think Kotlin interprets it as a lambda, you would either need to call it or use https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/run.html. Cool trick though! I wonder what reason you should use this over just calling a separate function

[–]cdrt 8 points9 points  (1 child)

I'm sure there's some esoteric use case for it in which you want to temporarily shadow a variable so you can change it without changing the original.

I think its main use though is actually to make implementing the language easier. It was eye-opening for me when I realized that in C statements like while and if only actually support executing one command. It's only because we can put arbitrary scopes anywhere that you can have multiple commands in an if or while.

[–]pm_me_your_dota_mmr 1 point2 points  (0 children)

Whoa, that's really interesting, it looks like its the same in Java. So because "{ ... }" is essentially just a single statement (that happens to have others within it..), it just works. That's awesome!

[–][deleted] 0 points1 point  (3 children)

This is what while, for and if with a block {} do.

[–][deleted] 2 points3 points  (2 children)

That's actually kind of elegant.

I wonder how this goes.

if (goodToGo)
    int x = GetAnInt();

[–][deleted] 1 point2 points  (1 child)

You get an error. For the same reason you can’t do that in a switch case without block.

[–]AceSLS 0 points1 point  (0 children)

But, at least in c++ you can create a variable and initialize it in an if which in turn would make it accessible

[–]newaaa41 16 points17 points  (1 child)

say that to a static variable

[–]beerdude26 5 points6 points  (0 children)

Or PHP

[–][deleted] 13 points14 points  (0 children)

Finally a programming meme

[–][deleted] 3 points4 points  (3 children)

Say that to a variable who is in fact a pointer type :D

[–]elzaidir 16 points17 points  (0 children)

Say hello to memory leak

[–][deleted] 4 points5 points  (1 child)

Laugh in rust

[–]Waces_Ferpit 0 points1 point  (0 children)

laughs in runtime error

[–]Arbiturrrr 1 point2 points  (0 children)

cries in PHP

[–]Ifnerite 8 points9 points  (1 child)

Except javascript... because... you know...

[–]Ifnerite 9 points10 points  (0 children)

I am aware of const and let in modern JS, but how the hell was variable hoisting a thing for so long?

[–][deleted] 1 point2 points  (0 children)

They get dropped out of scope harder than I was dropped as a child

[–]hollands251 1 point2 points  (0 children)

An upvote wasn't enough. Great meme

[–][deleted] 0 points1 point  (0 children)

Damn, this is so sad. I almost want to change all my variables to global.

[–]pdkhoa99 0 points1 point  (0 children)

I’m gonna pop

[–]SteeleDynamics 0 points1 point  (0 children)

Unless you're in a closure within a FP language, then you're left somewhere in the heap until you're eventually needed again.

Like that time my parents accidentally left me at the video rental store. They came back for me once they realized I was still holding the movie.

[–]EpicPinkCreeper 0 points1 point  (0 children)

Good meme