Usage of final let?


(Azuan) #1

Hi,

This is my first email to this mailing list. Do correct me if I did something wrong :slight_smile:

Read in docs, saying that by using final on variables, you can’t override it in it’s subclass. Using `final var` makes sense to me. But what is the purpose of using `final let` since by using `let`, it is already an immutable variable anyway.

Regards,
Azuan


(Jens Alfke) #2

Immutable isn’t the same as non-overridable. Immutable just means that its value in a single instance can’t be changed after initialization. But there’s nothing stopping you from creating a subclass that has a different value for that constant.

—Jens

···

On Jun 12, 2016, at 10:43 PM, Azuan via swift-users <swift-users@swift.org> wrote:

Read in docs, saying that by using final on variables, you can’t override it in it’s subclass. Using `final var` makes sense to me. But what is the purpose of using `final let` since by using `let`, it is already an immutable variable anyway.


(Karl) #3

Welcome!

That’s a fair point; “final let” doesn’t make sense. You should file a bug report on https://bugs.swift.org

Karl

···

On 13 Jun 2016, at 07:43, Azuan via swift-users <swift-users@swift.org> wrote:

Hi,

This is my first email to this mailing list. Do correct me if I did something wrong :slight_smile:

Read in docs, saying that by using final on variables, you can’t override it in it’s subclass. Using `final var` makes sense to me. But what is the purpose of using `final let` since by using `let`, it is already an immutable variable anyway.

Regards,
Azuan

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users


(Azuan) #4

I do undertand the difference. But would someone be kind enough to enlighten me with the correct example on how would you override a let variable in it’s subclasses and that you can prevent it by using the final modifier?

Regards,
Azuan

···

On 14 June 2016 at 9:59:16 AM, Jens Alfke (jens@mooseyard.com) wrote:

On Jun 12, 2016, at 10:43 PM, Azuan via swift-users <swift-users@swift.org> wrote:

Read in docs, saying that by using final on variables, you can’t override it in it’s subclass. Using `final var` makes sense to me. But what is the purpose of using `final let` since by using `let`, it is already an immutable variable anyway.

Immutable isn’t the same as non-overridable. Immutable just means that its value in a single instance can’t be changed after initialization. But there’s nothing stopping you from creating a subclass that has a different value for that constant.

—Jens


(Kevin Greene) #5

I tried making a simple example of a superclass with a let variable, and a
subclass that sets that variable to something different in its initializer.
It didn't compile. The let variable was not marked final either. So I don't
think adding final actually does anything for a let variable because there
is no way for a subclass to change its value anyways. Right?

···

On Mon, Jun 13, 2016 at 7:05 PM, Azuan via swift-users < swift-users@swift.org> wrote:

I do undertand the difference. But would someone be kind enough to
enlighten me with the correct example on how would you override a let
variable in it’s subclasses and that you can prevent it by using the final
modifier?

Regards,
Azuan

On 14 June 2016 at 9:59:16 AM, Jens Alfke (jens@mooseyard.com) wrote:

On Jun 12, 2016, at 10:43 PM, Azuan via swift-users <swift-users@swift.org> > wrote:

Read in docs, saying that by using final on variables, you can’t override
it in it’s subclass. Using `final var` makes sense to me. But what is the
purpose of using `final let` since by using `let`, it is already an
immutable variable anyway.

Immutable isn’t the same as non-overridable. Immutable just means that its
value *in a single instance* can’t be changed after initialization. But
there’s nothing stopping you from creating a subclass that has a different
value for that constant.

—Jens

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users