Why extensions must not contain stored properties? | Hindi tutorial

Поділитися
Вставка
  • Опубліковано 20 жов 2024

КОМЕНТАРІ • 73

  • @CodeCat15
    @CodeCat15  2 роки тому +9

    one question that developers may have is maybe adding stored property impacts the size of the memory, let's talk about it.
    Note: type = class/structure
    stored properties have state, they maintain their own values and from a responsibility standpoint it's not the job of extensions to maintain state to the type that it's extending and hence we have compile time errors.
    coming to the question of memory management
    if SUPPOSE adding stored properties was allowed in the extension, then this would have caused the entire object to be re-initialized OR maybe not
    since whatever is added to the type is decided during compile time
    the compiler will compile the code and will link everything to the type so that the task of memory allocation is smooth and done right
    so assuming that extensions could alter something on runtime or change memory structure which also is a runtime task is invalid, because before initialization we have the process of compilation where such checks are done
    it also raises a question that who manages the state of that new stored property? is it the type? is it the extension? because initially the type never had the new stored property to begin with, the extension added it
    the compiler just checks if the code that you added in extension, creates a new state or not and if it does then throw a compilation error
    so I look at it from what the extension can and cannot do, extension can add stateless routines but not the one that maintain state in this case stored properties.

    • @AbhishekSrivastava_ab
      @AbhishekSrivastava_ab 2 роки тому +1

      This is quite elaborate. Well explained. 👍🏻

    • @CodeCat15
      @CodeCat15  2 роки тому +1

      @@AbhishekSrivastava_ab Thank you, I am glad this was helpful.

    • @AbhishekSrivastava_ab
      @AbhishekSrivastava_ab 2 роки тому +2

      @@CodeCat15 There is another perspective to it if you think about it. Extensions give you the capability to add functionality to any type. Be it something we have defined or if it is defined in a framework, both dynamic or static. So it is quite convenient to say that, extensions can only add dynamic computations, where memory can be allocated anywhere and need not be contiguous. Hence stored properties can only be part of the concrete implementation and not loosely defined like as in an Extension.

    • @CodeCat15
      @CodeCat15  2 роки тому

      that's one more way of looking at it, thanks for sharing your views this is a nice insight as well, I really wished if apple could've taken efforts to mention few lines as to why this decision was made.

    • @AbhishekSrivastava_ab
      @AbhishekSrivastava_ab 2 роки тому +1

      @@CodeCat15 If Apple was really good at writing documents. You might have lost few subscribers 😉
      Take it as a blessing disguised.
      You're doing a great job here. Keep going.

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

    Very well explained

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

      Thank you, i am glad this video was helpful, please share the same with your ios groupand fell free to ask questions.

  • @AshwaniKumar-ew1cn
    @AshwaniKumar-ew1cn 2 місяці тому

    Apple needs people like you "Ravi" as we do 😅. Thanks a lot for such conceptual reasonings. Kash aap jaise seniors mile hote pehle!!!

  • @nidhitiwari634
    @nidhitiwari634 2 роки тому +1

    Thanks a lot Ravi for this video

    • @CodeCat15
      @CodeCat15  2 роки тому

      Glad it was helpful Nidhi 👍

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

    Outstanding.....🤩

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

    Hey Ravi…. Your way of explanation is really great. I have one small question.. do you know why we use get and set… if you have made a video on get set topic please send.. I could not found it in your playlist ..

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

    thank you for this amazing explanation

  • @AmitVLogsBharat
    @AmitVLogsBharat 5 місяців тому

    you can create static store property in extension .
    Like :- static let test = "Test"

  • @kalpatarusahoo1918
    @kalpatarusahoo1918 2 роки тому +1

    This question I asked by an interviewer

    • @CodeCat15
      @CodeCat15  2 роки тому +1

      Yes, it’s one of their favorite questions. I hope this video was helpful and do read the pinned comment of the video as it adds more inputs that I missed adding in the video.

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

    clear explanation. 👏👏👏

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

      Thank you, am glad it was helpful

  • @AdnanKhan-ug1qn
    @AdnanKhan-ug1qn 2 роки тому +1

    Good tutorial brother. Explained well.

    • @CodeCat15
      @CodeCat15  2 роки тому

      Thank you, am glad it’s helpful Adnan

  • @vishalmalvi895
    @vishalmalvi895 2 роки тому +1

    You made this easy to understand.
    Thank you so much

    • @CodeCat15
      @CodeCat15  2 роки тому

      Thank you Vishal 😊 I am glad this video was helpful.

  • @ranganathchenna4559
    @ranganathchenna4559 2 роки тому +1

    Appreciated .. we need these kind of in-depth explanation ..

    • @CodeCat15
      @CodeCat15  2 роки тому

      Thank you 😊 I am glad this was helpful

  • @poojaraghuwanshi1914
    @poojaraghuwanshi1914 2 роки тому +1

    Ravi did you created for KVO and KVC video ??

  • @mukeshhembrom231
    @mukeshhembrom231 2 роки тому +1

    Great explanation!!

  • @poojaraghuwanshi1914
    @poojaraghuwanshi1914 2 роки тому +1

    Ravi i faced many time software model interview question , but i did't found exact answer for it can you please make a brief discussion about it

    • @CodeCat15
      @CodeCat15  2 роки тому

      What was the question being asked in the interview?

    • @poojaraghuwanshi1914
      @poojaraghuwanshi1914 2 роки тому +1

      @@CodeCat15 Question is what type of software model you follow. in your project

    • @CodeCat15
      @CodeCat15  2 роки тому +1

      @@poojaraghuwanshi1914 the answer to this is related to what architecture you follow in your current application and why.

  • @poojaraghuwanshi1914
    @poojaraghuwanshi1914 2 роки тому +1

    @Ravi can you help me out for Coordinator exact purpose in swift

    • @CodeCat15
      @CodeCat15  2 роки тому

      I have a 3 part video on the coordinator pattern, I don’t recommend that pattern at all coz it just increases code complexity for no reason, search coordinator in my videos and you’ll see the videos also, do read the comments of each videos coz the comment section of this channel is a knowledge library of its own

  • @pavanmanjani7583
    @pavanmanjani7583 2 роки тому +1

    Wow, 😮 Keep Sharing

    • @CodeCat15
      @CodeCat15  2 роки тому +1

      Indeed, I hope the video was helpful. Do share with your iOS group 😊

    • @pavanmanjani7583
      @pavanmanjani7583 2 роки тому

      @@CodeCat15 Sure

  • @RupeshSaxena
    @RupeshSaxena 2 роки тому +1

    must be violating open close principle?

    • @CodeCat15
      @CodeCat15  2 роки тому

      Can you elaborate on this please, also check the pinned comment for more details on memory management of extension.

    • @RupeshSaxena
      @RupeshSaxena 2 роки тому +1

      @@CodeCat15 I mean to say if adding stateful properties in extension it will going to violate open/close principle and as per the open/close principle it is not a allowed to modify the existing implementation, extension act as adding new behaviour using existing states (be it computed properties or new behaviours manipulating existing states) and provide implementation based on new requirements.

    • @CodeCat15
      @CodeCat15  2 роки тому +1

      Perfect, yes that is also a case. Good explanation Rupesh.
      It also breaks SRP as adding states is only the responsibility of class & structs and not extensions so if suppose extensions did add states then now in a way we are violating SRP as well.

    • @RupeshSaxena
      @RupeshSaxena 2 роки тому

      @@CodeCat15 Yes it will also violate SRP..

  • @ranganathchenna4559
    @ranganathchenna4559 2 роки тому +1

    In few Interviews I faced this QN. Need answer and give explanation with any demo example for better understand
    I’m getting user data from api response
    Previously I’m getting key as “username”
    Now I’m getting key key as “userName”
    How you handle from ur end ..?

    • @CodeCat15
      @CodeCat15  2 роки тому

      if they response keys keeps on changing continuously then this means the api developers are not doing a good job in making a concrete response,
      a change in the response would mean you would have to change your contract as well,
      you could use the dictionary approach and add required checks on your side to check if the dictionary contains the following key i.e
      username, userName, name, UName, firstName, userFirstName, first_name, user_first_name and god knows how many more combination
      i guess by now you must realize why it's important to have a concrete keys in the response and why this is something that should not be handled on the iOS side.

  • @pushpender.s
    @pushpender.s 2 роки тому +1

    Amazing video and I don't know why this channel is underrated.

    • @CodeCat15
      @CodeCat15  2 роки тому +1

      Thank you Pushpender 🤗

  • @geetamsinghkushwaha8323
    @geetamsinghkushwaha8323 2 роки тому +2

    Best Swift Teacher in India ❤️

    • @CodeCat15
      @CodeCat15  2 роки тому +1

      Thank you for your kind words Geetam

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

    INITIALISATION issue could be the reason why extension can't contain stored properties. Say for example original class had one property and it is used by another classes in same module with one property initialiser, now if we add another property in extension then the classes using the one property initialiser would be required to add newly added property in extension, which would not be possible later on. I may not be quite right but it could be the one of reasons.

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

      Checkout the pinned comment of mine.
      No matter what usecase we are in, it all boils down to state management as explained in the video, if you are dynamically creating a container to hold state then the object has to be destroyed and re-constructed again.

  • @coinmeluha5358
    @coinmeluha5358 2 роки тому

    Thanks bro

  • @ravirock3067
    @ravirock3067 2 роки тому +1

    By the way great job ravi by making the concepts even more clear.

    • @CodeCat15
      @CodeCat15  2 роки тому +2

      Do checkout the pinned comment as well, I hope that too provides more insights to the topic

  • @prataprana2963
    @prataprana2963 2 роки тому +1

    As always a new and different and easy stuff / information about the topic.
    Thanku brother for this explanation.
    Also needs more stuff like this for ex:-
    Why open keyword can’t be use with struct.

    • @CodeCat15
      @CodeCat15  2 роки тому

      Thanks Pratap, yes this is indeed a good topic and perhaps questions like these are more of a brain teaser as they test your knowledge on the language and programming. I'll make a video on the open keyword, although I wanted to explain it in the comment but I think a code example and visuals would add more value

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

      Open Keyword works with reference types i mean with classes and overridable members of classes so using the open keywords with values types doesn't make sense so swift compiler will not like it , hence it will throw compile time error . Ravi brother waiting for the video and more technical reason on this topic . That being said thank you happy i coding !!!

  • @mandeepsinghbaath6892
    @mandeepsinghbaath6892 2 роки тому +1

    very useful Information about an extension💥💥 , bro please make video on MVVM two way binding

    • @CodeCat15
      @CodeCat15  2 роки тому

      Thanks Mandeep, I think am dropping the idea of two way binding as a video but perhaps can do a discord event on this, reason why am not making a dedicated video is coz two way binding does a lot and the code I did using observer pattern is not close enough to few of the things what two way binding does

  • @AmitKumar-mv3df
    @AmitKumar-mv3df 2 роки тому +2

    In my opinion, Extensions can't be initialized and that's why they can't hold stored properties. The extension is like a shadow of a type that gives some extra functionality but doesn't exist on its own.

    • @CodeCat15
      @CodeCat15  2 роки тому

      Very well said, the entire idea of extension is just to extend non-stateful behavior and anything that involves state will not be allowed by the compiler.

  • @divyantsrivastava9850
    @divyantsrivastava9850 2 роки тому +1

    I think extensions are run time so it will change the memory allocation if it allows the stored property in extension. That is the reason.

    • @CodeCat15
      @CodeCat15  2 роки тому

      I have a comment pinned about this, do check it out and let me know what do you think about it.

  • @ShuklaAmbuj
    @ShuklaAmbuj 2 роки тому

    Very well explained Ravi, thanks for it. Can you please tell us more about this State and Stateless and how does it work in Swift.

  • @AbhishekSrivastava_ab
    @AbhishekSrivastava_ab 2 роки тому +1

    It is to do with the memory allocation. When you make a class or a struct and declare a stored property inside it, the memory boundaries can be pre defined for each object that we declare later on.
    With computed properties, it does not completely become the part of the objects memory.
    How else can we manage the object memory.

    • @CodeCat15
      @CodeCat15  2 роки тому +1

      I have pinned a comment of mine on this, let me know what you think about it.
      Yes for a smoother memory allocation the compiler does a check for anything that can hold state in extension and then raises the compilation error.

  • @ranganathchenna4559
    @ranganathchenna4559 2 роки тому +1

    I'm speculating slightly here, but it could be to do with the amount of memory required to store an instance of an object.
    Adding functions and computed variables (which are basically just functions without arguments) don't require any additional memory to store an object. Adding a new stored variable requires additional memory to be allocated to store that object.
    It would make sense that this would be an issue if a class / struct was declared in a module, and then extended outside of that module to add an additional variable. I guess the module would need to be recompiled to be aware of the user's extension, which would break encapsulation and really defeat the whole purpose of a module. If it were pre-compiled this wouldn't even be an option.
    In my head it seems that it could be theoretically possible to extend a type to add additional stored properties in the same module that it is defined. The compiler should theoretically have enough information there to put all of the pieces together. Someone who knows a little more might be able to provide some more insight.

    • @CodeCat15
      @CodeCat15  2 роки тому

      I have pinned a comment of mine with regards an assumptions I have, tried to keep it relevant from programming standpoint but then would’ve been great if apple took efforts in mentioning this in the documentation than leaving it to the opinions/theories and assumptions.
      I think when you declare an extension we think that we are creating a boundary but I think there’s no boundary to begin with since all those extension functions can easily be accessed with that one object.
      We can try something what you mentioned and see how the compiler reacts to it, curiosity is a good thing testing that curiosity with code is even better.

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

    Hey Ravi…. Your way of explanation is really great. I have one small question.. do you know why we use get and set… if you have made a video on get set topic please send.. I could not found it in your playlist ..