0

As we know struct and enum both are value type. We can define constants Like:

struct Foo {
    static let constant = "SomeConstant"
}

print(Foo.constant)

enum Foo: String {
    case constant = "SomeConstant"
}

print(Foo.constant.rawValue)
  1. Which one would make sense based on comparison of memory allocation at runtime ?
  2. Since both seems to be type-properties for me, will they remain forever in stack memory till app is alive.
Nitesh
  • 1,924
  • 21
  • 31
  • 1
    You can go through this blog post: https://medium.com/@abhimuralidharan/difference-between-a-struct-and-a-class-in-swift-53e08df73714 or https://www.raywenderlich.com/7320-getting-to-know-enum-struct-and-class-types-in-swift. A quick Google search should fetch you your answer! – curiously77 Sep 30 '19 at 19:41
  • 2
    Your question is conceptual and an earlier duplicate can be found here: [Difference between struct and enum?](https://stackoverflow.com/questions/20680957/difference-between-struct-and-enum). Please read through that! :) – Sam Spencer Sep 30 '19 at 19:42
  • 2
    @SamuelSpencer even though that question explains the theoretical difference between structs and enums, Swift enums are much more powerful than the C/C++ ones and hence for this particular question, those differences between Swift enums are the C/C++ ones are quite important – Dávid Pásztor Sep 30 '19 at 19:46
  • Answer is given on link https://stackoverflow.com/questions/38585344/swift-constants-struct-or-enum is with case less constant, I have taken case with rawValue. I have read all these three articles, since I read few open source files of Swift itself related to enum there are many semantic methods calls of LLVM for enum. My question is basically from memory allocation aspects. I want to know which one would be optimised – Nitesh Sep 30 '19 at 19:59
  • 1
    That duplicate doesn't answer OPs question. – Alexander Sep 30 '19 at 23:19
  • 1
    @Nitesh Do you have a particular reason why you're worried about the memory layout of these two pieces of code? If there is any difference (which tbh I don't even think is worth bothering to check), it'll be so minuscule, that it'll be completely irrelevant. You should be picking the right tool for the job. If you need an enum, use an enum. If you need a set of static values, use a set of static values. – Alexander Sep 30 '19 at 23:25
  • @Alexander While code refactoring, We found there could be two ways to write constants. So we are little confused weather we should use static properties or caseful enum. so I am trying to find justification for one approach among this. – Nitesh Oct 31 '19 at 03:52
  • @Nitesh There's a difference, but it's about semantics, not memory layout. Enums are values that guarantee mutual exclusivity. If you need to store a traffic light's states, and want to do something with those state (e.g. making a `next()` function that takes you from one state to the next), that's the job of an enum. If you want to store a bunch of string constants, such as database column names, that's the job for static fields. – Alexander Oct 31 '19 at 14:04

1 Answers1

2

The Swift language doesn't have an official standard to refer to in cases like this. The memory layouts of these two pieces of code are implementation-defined, by the Apple Swift compiler, which is the de-facto standard for the language.

You can look at the emitted SIL or machine code, however, any observations you make are consequences of current implementation details, which are subject to change.

All that is to say: there's no reason why the compiler should handle these differently, but you can't rely on that to not change in the future.

Alexander
  • 59,041
  • 12
  • 98
  • 151