var, const, let... now ‘using’?

Поділитися
Вставка
  • Опубліковано 29 чер 2023
  • TypeScript 5.2 implements a new keyword - 'using' - which helps to handle explicit resource management in JavaScript.
    The 'using' TC39 proposal has reached Stage 3, meaning it's nearly ready to come to JavaScript - so TypeScript has added it in anticipation.
    Full article: www.totaltypescript.com/types...
    Become a TypeScript Wizard with my free beginners TypeScript Course:
    www.totaltypescript.com/tutor...
    Follow Matt on Twitter
    / mattpocockuk
    Join the Discord:
    mattpocock.com/discord

КОМЕНТАРІ • 555

  • @bozhidaratanasov7800
    @bozhidaratanasov7800 Рік тому +811

    So JS introduced "using", while C++ introduced "import"... What a time to be alive!

    • @suuunly
      @suuunly Рік тому +54

      and C# has had it for ages

    • @SpiritmanProductions
      @SpiritmanProductions Рік тому +18

      And VB has had it for decades.

    • @coldestbeer
      @coldestbeer Рік тому +8

      @@suuunly that's not a lot in c# years

    • @thelatestartosrs
      @thelatestartosrs Рік тому +65

      i read this "What a time to be alive!" in Two Minute Papers voice

    • @yeetskeet691
      @yeetskeet691 Рік тому +16

      @@suuunly C# = Microsoft Java

  • @hicoop
    @hicoop Рік тому +297

    Sounds cool but still waiting for that Pipe | operator

    • @vikingthedude
      @vikingthedude Рік тому +15

      Yeah I’m looking forward to pipe and iterator method chaining

    • @AngelHdzMultimedia
      @AngelHdzMultimedia Рік тому +7

      It's taking too long! I checked the status recently... and while at it, I played creating my own pipe function (like rxjs).

    • @no_mnom
      @no_mnom Рік тому +4

      rxjs has that basically

    • @JonathanRose24
      @JonathanRose24 Рік тому +11

      I feel like at this rate I’ll retire before being able to use the pipe operator…

    • @timotiussimanjuntak9148
      @timotiussimanjuntak9148 Рік тому

      ​@@JonathanRose24😂

  • @TimOkolowski
    @TimOkolowski Рік тому +138

    This is actually pretty useful for WebAssembly libraries that expose objects that need to be manually memory managed. The generated bindings to Rust code have classes with a free() method. So the end user needs to be aware to to call it when the value isn’t needed anymore. Maybe this can even be unnecessary when WASM garbage collection lands, but I think for now using will be a nice improvement.

    • @zzzyyyxxx
      @zzzyyyxxx Рік тому +6

      Reminds me of Drop in Rust

    • @anonymousalexander6005
      @anonymousalexander6005 Рік тому +17

      @@zzzyyyxxxIt’s more akin to Python’s ‘with’, but it’s all scope hooks anyways so yeah.

    • @dealloc
      @dealloc Рік тому +2

      One example is the library, Shiki, which relies on WASM bindings for a regular expression, Oniguruma. But it failed to dispose the objects (by manually call free/dispose) and resulted in a memory leak. Had Symbol.dispose and `using` existed already, it would've saved a ton of debugging.

    • @_sorashi
      @_sorashi Рік тому +2

      It's literally like C# using. Used for unmanaged resources too. Async/await was in C# first too

    • @Rope257
      @Rope257 Рік тому +1

      @@_sorashi Isn't the same language-architect that did C# now heading Typescript?
      Also, this reminds me of Java's Autoclosable + try-with-resources but with the single keyword as syntactic sugar.

  • @kaycooking
    @kaycooking Рік тому

    Love the new background, happy to see you back posting videos!

  • @carlosmspk
    @carlosmspk Рік тому +152

    Python has context managers, C++ does it with RAII, Java also has similar with try(resource) and Kotlin later implemented a more clean approach
    I'm surprised that javascript took so long to implement this, but I'm glad they did!

    • @Moody0101
      @Moody0101 Рік тому

      I was about to say thta lol

    • @JosueRodriguez08
      @JosueRodriguez08 Рік тому +4

      And golang have defers, please don't forget about golang 😢

    • @untheo
      @untheo Рік тому +1

      @@JosueRodriguez08 Swift also uses defer

    • @ihateidiots9484
      @ihateidiots9484 Рік тому +4

      They did it in the most stupid way. 🤢🤮

    • @carlosmspk
      @carlosmspk Рік тому +1

      @@JosueRodriguez08 I have never used Go but defers are a bit different, since you still have to explicitly call open and close (I think?)

  • @shapelessed
    @shapelessed Рік тому +33

    TLDR: We move the process of "cleaning things up" from outside the function inside it.

    • @Tjommel
      @Tjommel 11 місяців тому

      so.. basicly they added an destructor that most other language already have..

  • @gonsalomon
    @gonsalomon Рік тому

    Your new setup looks tidy and neat. Congrats!

  • @austincodes
    @austincodes Рік тому +14

    use useUsing using use

  • @jackdavenport5011
    @jackdavenport5011 Рік тому +1

    This is awesome, I often find some of my messiest code comes from writing IO and having lots of connections and objects which need to be closed/released after you're done with them. This keyword pretty much resolves that.

  • @T_nology
    @T_nology Рік тому +4

    Thanks for the information! This seems like a pretty cool concept, although I'll need to wrap my head around it a little bit more to fully understand it.

  • @TroyNiemeier
    @TroyNiemeier Рік тому +103

    "Is JS trying to turn JS into C#?" YES! That's precisely what's happening, as well as every other language with good useful features.
    We've reached a point in technology where ALL languages are adopting the best ideas of all of the other languages to popularize and fortify their offerings.

    • @iooosef6006
      @iooosef6006 Рік тому +3

      I'm not sure but wasn't async await promise based from C# async await?

    • @TroyNiemeier
      @TroyNiemeier Рік тому +3

      ​@@iooosef6006, I mean, it's a generic functionality, but C# uses the `Task` object and `Task` to communicate what is awaiting a response, not a "Promise" per se. But I think they have similar methods and options.

    • @ScottBlomquist
      @ScottBlomquist 11 місяців тому

      @@iooosef6006 Yep. C# originated the use of async/await keywords in 2012. However, the semantics (and underlying structures) are slightly different between any of the implementations with which I'm familiar (Python, Rust, C#, now JS).

    • @takemyhandtakemymind8039
      @takemyhandtakemymind8039 11 місяців тому

      Not sure if this is the „best idea“ which should be adopted.

  • @SamsButterscotch
    @SamsButterscotch Рік тому

    Brilliant video!

  • @ApexJnr
    @ApexJnr Рік тому

    Well thank you for the update, here's a sub!

  • @PostMeridianLyf
    @PostMeridianLyf Рік тому

    You got my sub! Love coding content and we need more TS gurus

  • @qodesmith520
    @qodesmith520 Рік тому +16

    Thanks for the video! Only critique on the new setup is the audio. There's a decent amount of room sound being captured. I'm sure you're still putting things together, but in case it wasn't on your radar, there's a new free de-reverb plugin out called Goyo. Might help with the post production.

    • @borstenpinsel
      @borstenpinsel Рік тому +3

      De-reverb. My god we're truly living in the future aren't we?

    • @mattpocockuk
      @mattpocockuk  Рік тому +3

      Thanks! I have a setup fix for this coming. Agree that it's _just_ outside the bounds of acceptibility.

  • @hey.im_him
    @hey.im_him Рік тому +28

    Nice to see more c# features coming to JavaScript. Assuming the connection is Typescript being built by the same person behind c#

    • @yt-1337
      @yt-1337 Рік тому +1

      yeah i really miss "using" now that i switched from c# to kotlin (i mean i love both languages and kotlin has .use {} which is nice too but just not the same sadly)

  • @artyomtaranenko2267
    @artyomtaranenko2267 Рік тому

    Matt we need more videos).
    U r best of the best )

  • @raenastra
    @raenastra Рік тому +58

    I've always wished JS would have something like Python's context manager. Amazing. Generator functions were always awkward for this

  • @Fullflexno
    @Fullflexno Рік тому

    Good stuff!🎉🎉

  • @hakuna_matata_hakuna
    @hakuna_matata_hakuna Рік тому

    great video , nice clean, minimal setup

  • @mrarazvi
    @mrarazvi Рік тому

    Nice! Thanks for sharing.

  • @Dozer456123
    @Dozer456123 Рік тому +3

    Honestly a pretty subpar solution for something that has already been solved in Go for example with the defer statement. More complex, worse syntax, does basically the same thing.

  • @hut_fire1490
    @hut_fire1490 Рік тому

    Miss you matt !!

  • @utkutekalmaz
    @utkutekalmaz Рік тому +2

    Congratz with the new place Matt!💙
    I feel like you can add something that handles the teeny tiny echo-ish thing

    • @mattpocockuk
      @mattpocockuk  Рік тому +1

      Agree, there's a ton of empty space on the walls around me so that can't be helping. Overall happy with it (I don't think my viewers listen on studio-quality headphones so I can get away with it being slightly crap) but as I add more furniture to the office I think it'll get better.

    • @utkutekalmaz
      @utkutekalmaz Рік тому

      @@mattpocockuk Even if you have the worst sound system on youtube, you can be sure that we will enjoy watching it! 😄

    • @invinciblemode
      @invinciblemode Рік тому

      @@mattpocockukI’m just listening on my shitty iPhone speakers and the reverb is still pretty bad. Thanks for the great content as usual.

    • @mattpocockuk
      @mattpocockuk  Рік тому

      @@invinciblemode Thanks! That's useful. I have a setup fix coming this week.

  • @axelayala5275
    @axelayala5275 Рік тому

    We love you Matt

  • @typingturtle5155
    @typingturtle5155 Рік тому

    Thanks, great article! Just a heads up the audio sounds a bit echo-y compared to previous videos.

  • @m4heshd
    @m4heshd 11 місяців тому +1

    My immediate thought: "Rust's drop in JS? I'll take it"

  • @DanelonNicolas
    @DanelonNicolas Рік тому

    I love this "news" videos ^^ ❤

  • @mhmdalharbi2370
    @mhmdalharbi2370 Рік тому +15

    Sounds like defer keyword in Go but without special symbols:
    const conn = ....
    defer conn.close()
    This will be called after function execution and before throwing any exceptions.

    • @ndykhng
      @ndykhng Рік тому +11

      One difference is `using` runs on block boundary, whereas `defer` run on function boundary. Placing `defer` on a loop doesn't run it every loop, but stacks them up and runs once the function ends.

  • @nagarajansubramani
    @nagarajansubramani Рік тому +2

    So what is TypeScript's _using_ going to be transpiled into? What will the native JS code look like?

  • @belkamax05
    @belkamax05 Рік тому

    It does make sense, I came to FE from DotNet long ago, and I've been missing this feature. In Typescript we now deal it on our own.

  • @minikame2272
    @minikame2272 Рік тому +1

    Other than syncronous thenables, what would be the reason for having separate sync and async disposal symbols instead of inferring it from the syncronicity of the returned function?

  • @catwhisperer911
    @catwhisperer911 10 місяців тому

    Nice addition to the language. Thanks for sharing.

  • @AllenTsuna
    @AllenTsuna Рік тому

    Also nice for handling sockets etc, nice

  • @BRP-Moto-Tips
    @BRP-Moto-Tips Рік тому

    Really exciting, i dont know if it would be mass adopted, but im sure the libraries will and it'll benefit everyone aboard, great news!

  • @ridhwanio
    @ridhwanio Рік тому

    Very interesting, I wonder if this could be used to manage state on the front end

  • @alon.galperin
    @alon.galperin Рік тому

    didn't know about symbol keyword, thnaks

  • @IvanRandomDude
    @IvanRandomDude Рік тому +5

    Why would we close db connection? In all real world apps there is connection pool and connections are not being closed after work is finished, rather they are reused by other operations. This eliminates overhead of creating and disposing connections over the wire all the time.

    • @jrmc732
      @jrmc732 Рік тому

      You are not very up do date my friend, closing connections is very common in most apps.

    • @IvanRandomDude
      @IvanRandomDude Рік тому

      @@jrmc732 Why is that better?

    • @Kevin-qz4eq
      @Kevin-qz4eq Рік тому

      @@jrmc732 its only common if you are using transactions, otherwise drivers will provide a convenience method on the pool that releases the connection for you.

    • @MrMudbill
      @MrMudbill Рік тому

      Sometimes you need to close connections. If you don't, you'll introduce issues. Perhaps an example could be a cron job that only does one task and then is done.

    • @FilipCordas
      @FilipCordas Рік тому

      You still need to tell the connection pull you are done with using the connection so it can go back to be used by something else.

  • @irlshrek
    @irlshrek Рік тому

    I cant believe you havent done a video on ts-pattern yet!

  • @Luxcium
    @Luxcium Рік тому

    *Explicit Resource Management* wow that is what I need in NodeJS it was tricky to manage some stuff to close and such...

  • @user-hn1ph6ry8l
    @user-hn1ph6ry8l Рік тому

    Ok, sound useful thing. Thanx for video :)

  • @zackyezek3760
    @zackyezek3760 Рік тому +10

    Ah, so JS is introducing a keyword to do RAII.
    That’s a good idea, especially since the JS interpreter itself is usually written in C/C++ with reference counting and RAII. So it’ll be easier to directly map the backend implementation to the scripting, there’ll be an explicit destructor you can use for things like I/O flushing, and (esp. complied) JS will pick up one of the core features that enables high performance in languages like c++ and Rust.
    My only mild criticism is that “using” might’ve been a suboptimal keyword choice for this. Since your exact intent is to have a scoped variable, why not “scoped”?

    • @demenevdmitriy4175
      @demenevdmitriy4175 Рік тому +1

      Scoped is longer to type and `using` keyword already exists. Why invent a new keyword?

    • @danielegvi
      @danielegvi Рік тому +3

      "JS interpreter itself is usually written in C/C++ with reference counting and RAII." Would love a source on this, so far it seems all major JS interpreters/compilers use tracing garbage collection, not reference counting. This allows JS values to have cyclical references and still be able to be GC'd.

  • @albiemer
    @albiemer 11 місяців тому

    powerful speaker... i like the way you speak..

  • @carlosricardoziegler2650
    @carlosricardoziegler2650 Рік тому

    Nice I saw your post on LinkedIn about that :) thanks for sharing:)

  • @jordanbenyon9375
    @jordanbenyon9375 Рік тому +20

    The DB example looks hard to read and a little bit more verbose than a simple try catch.
    Probably won't use this that much but it does look neat.

    • @lengors7327
      @lengors7327 Рік тому

      If you use it only on one place yeah, but if you are using it in multiple places in your code its worth it.
      Even more so if you create a factory function that generates the connection resource from the get db function
      Edit: or your getDB function already creates an object with that feature in mind (e.g. the close function is replaced with the dispose one)

    • @bobthemagicmoose
      @bobthemagicmoose Рік тому

      It moves the "cleanup" code to be with the item itself. The setup looks a little weird at the moment, but using using (har har) will be far more clean and will help prevent unused resources from still being reserved. Essentially, the library or factory can include the cleanup code so you won't even see it. Also, as the other commentator said, it will be something you write once but reuse in multiple places. Many other languages have some auto-cleanup technique so it makes sense this would be added to JS.

  • @farahrayis5928
    @farahrayis5928 Рік тому +20

    "using" functionality is a great addition to typescript, thanks for the straightforward and easy to understand explanation. You are a very good teacher.
    The setting is nice and cool, a picture or two two at the back would be great, can't wait to see what you'll choose.

  • @MrEnsiferum77
    @MrEnsiferum77 Рік тому

    @Matt what is today wrong with async await and try catch when reading files, and what symbol.dispose solving for the same case?

  • @michalis2942
    @michalis2942 Рік тому

    C# has using which lets you initialize a variable and sets a scope, when operations inside that scope are finished that variable is disposed.

  • @forivall
    @forivall Рік тому +3

    This concept also exists in python with the "with" keyword

  • @sabuein
    @sabuein 11 місяців тому

    Thank you.

  • @richardantao3249
    @richardantao3249 Рік тому

    He's back!!

  • @vaskoa
    @vaskoa Рік тому

    Hey, great. Can't wait to abuse this to the fullest 🙃

  • @mumk
    @mumk Рік тому

    very cool implementation of IDisposable

  • @LoveLearnShareGrow
    @LoveLearnShareGrow Рік тому

    This would be perfect for closing AWS Xray segments. Wish I could use it now!

  • @i74ifa
    @i74ifa Рік тому

    everything is nice in this video❤

  • @vividwater
    @vividwater Рік тому +1

    I'm intrigued about an important part: catching and handling errors in such scenarios. Looks like we still have to wrap it in a try/catch? I didn't see error handling in this demo.

  • @mistersunday_
    @mistersunday_ Рік тому

    Thanks for keeping the rest of us current :)

  • @edgeeffect
    @edgeeffect Рік тому +11

    I'm at this very moment (well, this week anyway) trying to get my head round this same concept in Python... With your explanation of how it works (or will work) in TS/JS, I might have grocked it in Python... we'll see in the morning when I get back to my Python code. (sorry for saying "Python" so many times... I'll go wash my mouth out)

    • @baja
      @baja Рік тому +4

      With blocks using a class with __enter__ and __exit__ does this in python

    • @ZWZDOzLtxBEO
      @ZWZDOzLtxBEO Рік тому +2

      context manager

    • @edgeeffect
      @edgeeffect 11 місяців тому

      Yeah... context manager

  • @mfpears
    @mfpears Рік тому +1

    Would be nice for class properties too

  • @nikilk
    @nikilk 11 місяців тому +1

    Hi Matt, thanks for that.. Interesting how using will enable a whole set of capabilities in the JS land..
    I know tensorflow has something similar like tf.tidy() builtin that kind of takes care of gc for the tensors. Using sounds like a similar native capability.
    I started of in C# and moved to JS/TS too just as jQuery was becoming a thing :)

  • @Peregringlk
    @Peregringlk Рік тому +1

    Javascript 30 years later to the idea of automatic object destruction tied to a scope.

  • @JimishFotariya97
    @JimishFotariya97 Рік тому

    Would it work with classes? like a cpp destructor? 🤔

  • @baka_baca
    @baka_baca Рік тому +17

    Sweet! While this looks much clumsier than what C# has, this does look like a nice improvement.

  • @Quillraven
    @Quillraven Рік тому

    So like try-catch with resources in Java and the Autocloseable interface?

  • @dasten123
    @dasten123 Рік тому

    So when does the dispose function get called? On garbage collection or before?

  • @liqo12
    @liqo12 Рік тому +1

    does dispose execute after a sigint?

  • @thomblweed
    @thomblweed Рік тому +1

    I remember using this in C# years ago 🙂

  • @joeygough5374
    @joeygough5374 10 місяців тому +1

    It is like a useEffect in react, where you return the function to be called when the component unmounts.

  • @dopecello8670
    @dopecello8670 Рік тому

    C# plus TypeScript about to be the new badass stack

  • @johnblack8178
    @johnblack8178 Рік тому

    sorry for being a noob but could be using be used also for subscriptions?

  • @deepfuchsia7204
    @deepfuchsia7204 Рік тому

    I recall it from my C# experience many years ago

  • @jongeduard
    @jongeduard Рік тому +16

    As we see here, modern additions to JavaScript are quite frequently influenced by C# and DotNet.
    Not many years ago, Java eventually also added a simular kind of solution, but in a different way, by integrating it into the try catch finally notation, the so called try with resources.

    • @sadsongs7731
      @sadsongs7731 Рік тому

      Except C++ was using RAII in 1984. Python's With statement dates back to 2005 and try/finally was already in the language. Java's try/finally predates C# 'using' and probably C# as a whole.

    • @jongeduard
      @jongeduard Рік тому +1

      @@sadsongs7731 I was talking about the try-with-resources variant, which exists since Java version 7 and 8, but not earlier. The old try catch finally had nothing to do with RAII or resource cleanup.

  • @ThEldeRS
    @ThEldeRS Рік тому +14

    What would happen if your dispose or asyncDispose fail? Also, what about dangling references? Are they handled on the compiler level? Seems like a lot of things to think about and not necessarily preventing problems as much as it’s creating new ones, but maybe I’m wrong.

    • @doc8527
      @doc8527 Рік тому

      Your question can be asked for every language even system that has the similar functions. For language/code you write/use, The short answer is probably there is nothing you can do about other than reattempting the failing dispose (of cuz system is totally different, they built tons of stuffs to mitigate/negate this problems). But what happen if the reattempt got cut off for any reasons (like power off)? Whether it will be cleaned after a while by extremely smart GC. Or it will stay there forever until at some points someone have to reboot the server (in web client side case, refresh the website) then nothing will be left.
      Your question is pretty similar to what happen to the halfway transferring files if you are transferring files but someone unplugs the usb. The short answer is the files at the "exact moment" of transferring will be damage or lost no matter what. We just need to make sure the original and the already stored files in USB wouldn't be corrupted.
      For most of the cases, your question doesn't matter, but for financial/bank (not sure about the embedded code), they have tons of guards, multiple trustful sources, backups to validate/invalidate the potential corrupted data that is caused by your problem.

    • @ThEldeRS
      @ThEldeRS Рік тому +2

      @@doc8527 Seems like a lot of examples that are very significant. You can't just say "it doesn't matter cause I'm not working in a bank/something else important". To me, every line of code that I write is important even if I'm writing a fart joke generator. And of course, you can ask this question for every language/code that does it, and I do. For the most part the answer is either "we didn't think about it" or "we don't care" or "this is an edge case". To all three my response is you should care and you should think about it. What happens if this new using and dispose functionality is written poorly and is mutating some wider scoped variable? What if the GC already cleaned it? This is incredibly important because that is a security breach waiting to happen with dangling references. I'm not saying it's not useful, or even that I won't use it, I'm merely asking what would the TS compiler do to prevent such obvious problems? Would it work like the Rust borrow checker and make it virtually impossible to create dangling references? Would it just not care? This functionality is very useful exactly in the areas that you yourself have defined as important, so I think it's very important to talk and understand what would be the consequences of this failing and not working with the happy path.

    • @mattpocockuk
      @mattpocockuk  Рік тому +4

      @@ThEldeRS This is discussed in the TC39 proposal!

    • @BrotherCheng
      @BrotherCheng 11 місяців тому

      Why would dangling reference matter? If you have a dangling reference it would have been an issue/bug in the old style of doing resource freeing using try/finally anyway. JS doesn't fundamentally solve this problem in its current design and if you care about it, you should use Rust or wait for JS to have a borrow checker. Either way dangling reference is orthogonal to this new feature.
      If your dispose fail, I would imagine you handle it just like you handle it in the past? This feature isn't providing a fundamentally different way of programming, but rather providing a more ergonomic and foolproof way of invoking a dispose function.

    • @whatthepeople
      @whatthepeople 10 місяців тому

      This feature is about to lower coupling. When you work with some resource you have gotten, you basically want not to know how to dispose it. Disposing should be on another level of an application, on the same level where getting a resource is implemented. Thus, by itself the feature neither adds new issues with handling a failed dispose nor creates a new approach to do that.

  • @uziboozy4540
    @uziboozy4540 Рік тому

    Can Symbol.dispose be used in conjunction with objects in WeakMap?

  • @supernenechi
    @supernenechi Рік тому

    Looks like a roundabout way of using a defer statement like you have in Go?

  • @nyashachiroro2531
    @nyashachiroro2531 Рік тому

    Great video. I think I heard a bit of echoing in the new office.

  • @handlebest
    @handlebest Рік тому

    what does dispose mean in this context?

  • @alexanderkomanov4151
    @alexanderkomanov4151 Рік тому

    Wow! Cool!

  • @omarmarzougui2901
    @omarmarzougui2901 9 місяців тому

    When we do functional programming and therefore without mutation we create more instances, is this useful in this case to perfect the behavior of the grabage-collector to consume less memory?

  • @johnflux1
    @johnflux1 Рік тому +2

    My biggest question - will it warn if I *don't* use using, but the function I call returns a dispose function?

    • @mattpocockuk
      @mattpocockuk  Рік тому +3

      Also a big question for me. I am pretty sure it'll be possible to lint this even if TS doesn't support it natively (which I bet it will).

  • @irlshrek
    @irlshrek Рік тому

    this is good to know for when I have to use ts/js! tbh javasript-fatigue is a real thing

  • @patrickmiharisoa5501
    @patrickmiharisoa5501 Рік тому +1

    Very useful with RAII

  • @sc76399
    @sc76399 Рік тому

    So to me its like a deferred function in golang? that is mostly used to clean up /close file handlers and database connections at the end of the scope they were instantiated.

  • @minimoments911
    @minimoments911 Рік тому

    Is this like defer in go?

  • @mikopiko
    @mikopiko Рік тому

    So the "using" keyword and resources is used to handle the lifecycle of an object?

  • @MinorMood
    @MinorMood Рік тому +2

    thanks for the video) The concept itself though - not sure if it comes any handy, makes the code less explicit, in my opinion.

  • @Berkeli
    @Berkeli Рік тому

    Thank you JavaScript for making me love simpler languages such as golang even more

  • @miguelrodriguezcimino1674
    @miguelrodriguezcimino1674 11 місяців тому

    As soon as he showed the first piece of code I thought "oh so it's like C# but with more complicated syntax"

  • @rafaaferid1789
    @rafaaferid1789 Рік тому

    Is it like "with" in Python?

  • @infiniteone783
    @infiniteone783 Рік тому +1

    Wouldn't an alternative for the DB.disconnect() dispose be good for a try catch too? Idk, I see use for "using", but I don't see where it would be 100% better with something else. Does seem cool the [Symbol.Dispose] concept though.

    • @FilipCordas
      @FilipCordas Рік тому +1

      C# had this for a long time it's excellent.

    • @harrytsang1501
      @harrytsang1501 Рік тому

      Python has it too and it’s excellent when you know you need to borrow a resource but only got a section of code.
      The best part is that this programming interface also works with other resources. for python you can do anything from opening a file to setting up the context to use GPU. If it throws or finish execution, you don’t have dangling opened files or connection

  • @naakaah
    @naakaah Рік тому +8

    Great video 👌 Could have mentioned that Anders Hejlsberg is the father of TypeScript & C# 🙂

    • @mattpocockuk
      @mattpocockuk  Рік тому +1

      He is, but he's not involved with this proposal.

    • @naakaah
      @naakaah Рік тому

      @@mattpocockuk Agree. It was just to remind people why Typescript & C# share so many features 🙂

  • @YaohanChen
    @YaohanChen Рік тому

    Does the resource get disposed as soon as the variable goes out of scope, or is it when the object can be garbage collected? What happens if you pass the resource variable to an async function or reference it in a closure, which uses the resource later?

    • @jotch_7627
      @jotch_7627 Рік тому

      it is tied to scope, not garbage collection. as for passing it to a closure or storing it somewhere, i gather (though i have not verified) that the object will still have its dispose method called when the initial binding goes out of scope. thats the main point of it being explicitly managed. if you wanted to do cleanup after everybody is definitely done using it, thats where implicit management with a finalization registry can come in handy

  • @DerEingefrorene
    @DerEingefrorene Рік тому

    So... They just reinvented the destructor...

  • @nyverinorlyth9555
    @nyverinorlyth9555 Рік тому

    So this is the drop trait from rust?

  • @driRochedo
    @driRochedo Рік тому

    They are features imported front c#

  • @Mrz355
    @Mrz355 Рік тому

    What about exceptions? Let's say I get exception while working with db - would Symbol.Dispose-fun still be invoked afterwards?

  • @Gigusx
    @Gigusx Рік тому +1

    Looks pretty cool but would take a while for me personally to develop intuition for it.

  • @Wrigggy
    @Wrigggy Рік тому

    so it's like, you add your own function to the garbage collector? Am I thinking of that correctly?

  • @kinwong8618
    @kinwong8618 Рік тому +2

    C# has using of IDisposable since forever.

  • @peybro
    @peybro 11 місяців тому

    Is it similar to Java's Try-With?