Swift: why aren't all variables lazy by default?

William Entriken

In comparing these two options for defining an instance property:

var networkManager = NetworkManager.sharedInstance()

var lazy networkManager = NetworkManager.sharedInstance()

Both:

  • Can evaluate a block to get the value
  • Can be declared inline (not a block, like above)

Lazy:

  • Can refer to self
  • Is not calculated until needed
  • If you don't use it, it is never calculated

Non-lazy:

  • No benefits whatsoever

It appears that there is no benefit to ever use a non-lazy variable. So why does the language allow the programmer to make this inferior choice?

(I am NOT asking about the difference between var and let à la Are Swift constants lazy by default?)

leeor

One reason might be that lazyness is not well-suited for situations where you want control when the evaluation happens. this is relevant in cases where the work being done in the assignment has side effects.

Although this pertains to closure, this blog post by stuart sierra explains this idea very well, and I think it applies equally in any language.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why aren't Enumerators in Ruby (2.0 +) lazy by default?

From Dev

Why aren't SAS Macro Variables Local-Scope by Default?

From Dev

Why aren't multimedia codecs included by default?

From Dev

Why aren't all functions inlined?

From Dev

Why aren't all applications 'portable'?

From Dev

Protected Variables aren't allowed by default in Checkstyle, but what about in an enum?

From Dev

Why aren't C++ types const by default?

From Dev

Why aren't include guards in c++ the default?

From Dev

Why aren't DIVs with given widths including paddings and borders by default?

From Dev

Why aren't third-party cookies disabled by default?

From Dev

Why aren't there existentially quantified type variables in GHC Haskell

From Dev

Why aren't my class variables being set?

From Dev

Why aren't variables like $PS1 in printenv?

From Dev

In Ruby, why aren't variables not interchangeable within code blocks?

From Dev

Why aren't both Double variables updated with same value?

From Dev

Why aren't all my processes starting at once?

From Dev

Why aren't the inferred types of these Haskell functions all the same?

From Dev

clojure pmap - why aren't i using all the cores?

From Dev

Why aren't all Mac applications easily portable to Linux?

From Dev

Why aren't all the base classes constructors being called?

From Dev

Ace Editor Javascript: How to disable all extra commands that aren't default in textarea?

From Dev

Why aren't these vectors equal?

From Dev

why aren't primitives nullable

From Dev

Why aren't these two equal?

From Dev

Why this lines aren't working?

From Dev

Why aren't "NOT IN" and "NOT EXISTS" working?

From Dev

Why aren't these ACLs working?

From Dev

Why aren't there enough inputs?

From Dev

Why aren't C++14 standard-defined literals in the global namespace by default?

Related Related

  1. 1

    Why aren't Enumerators in Ruby (2.0 +) lazy by default?

  2. 2

    Why aren't SAS Macro Variables Local-Scope by Default?

  3. 3

    Why aren't multimedia codecs included by default?

  4. 4

    Why aren't all functions inlined?

  5. 5

    Why aren't all applications 'portable'?

  6. 6

    Protected Variables aren't allowed by default in Checkstyle, but what about in an enum?

  7. 7

    Why aren't C++ types const by default?

  8. 8

    Why aren't include guards in c++ the default?

  9. 9

    Why aren't DIVs with given widths including paddings and borders by default?

  10. 10

    Why aren't third-party cookies disabled by default?

  11. 11

    Why aren't there existentially quantified type variables in GHC Haskell

  12. 12

    Why aren't my class variables being set?

  13. 13

    Why aren't variables like $PS1 in printenv?

  14. 14

    In Ruby, why aren't variables not interchangeable within code blocks?

  15. 15

    Why aren't both Double variables updated with same value?

  16. 16

    Why aren't all my processes starting at once?

  17. 17

    Why aren't the inferred types of these Haskell functions all the same?

  18. 18

    clojure pmap - why aren't i using all the cores?

  19. 19

    Why aren't all Mac applications easily portable to Linux?

  20. 20

    Why aren't all the base classes constructors being called?

  21. 21

    Ace Editor Javascript: How to disable all extra commands that aren't default in textarea?

  22. 22

    Why aren't these vectors equal?

  23. 23

    why aren't primitives nullable

  24. 24

    Why aren't these two equal?

  25. 25

    Why this lines aren't working?

  26. 26

    Why aren't "NOT IN" and "NOT EXISTS" working?

  27. 27

    Why aren't these ACLs working?

  28. 28

    Why aren't there enough inputs?

  29. 29

    Why aren't C++14 standard-defined literals in the global namespace by default?

HotTag

Archive