#2637 Fields and local fields by default is immutable

danykey Mon 11 Sep 2017

Hi,

Thanks for Fantom!

So, I am a bit confused because F not supports const(read-only) for local vars. If we talk about safe programming so it should has been implemented.

My experience says to me that this is bad style:

1)

test := 1;
for (...) {
  if(...) {
    test = 2;
  }
}
return test

2)

Mutable vars may be initialized mistakenly. We need know exactly that var won't initialized again!

In most cases vars should be read-only by design. It's good style!

I suggest make all var as read-only by default. If need mutable(in least cases) then we will set additional symbol.

For example:

Srt readonly := "foo"
Srt! mutable := "bar"

init(readonly, mutable)

Void init(Str readonly, Str! mutable) {
  readonly = "cat" // Error!
  mutable = "dog" // Ok
}

Also I suggest to remove const keyword from lang at all. I know that the compiler uses const classes but this flag may to hide to bytecode. If class is not contains mutable fields then set hidden const flag.

Second easy way is allow const for local vars:

Void test(const Str readonly, Str mutable) {
  const Int a := 5;
  a = 3 // error
}

Thanks.

go4 Thu 14 Sep 2017

I think the Fantom assumes you never make mistake at a local scope.

But the const for local vars is useful for closure. It's difficult to know a closure is mutable or not, until runtime:

Void main()
{
  Int i := 1
  fun := |->| { echo(i) }
  ++i
  echo(fun.isImmutable)
}

SlimerDude Wed 27 Sep 2017

Hi danykey!

Thanks for the idea - read-only local vars is something I've not previously thought of!

Fantom is often touted as a pragmatic language and is liked because it eloquently solves a lot of day to day problems. And to be honest "mistakenly re-assigning a local variable" is not a problem, I personally, can ever remember having. (Not to say it isn't a problem to some.)

Whereas sharing data between threads is a known and common problem in concurrent programming, and Fantom solves this decisively with immutability and const classes baked into the core syntax.

I suggest make all var as read-only by default. If need mutable(in least cases) then we will set additional symbol.

Being practical, this would be a huge breaking change and would invalidate (about) every line of Fantom code every written! So it's definitely not going to happen in Fantom v1 - but it may have the potential for discussion in Fantom v2.

But note that this suggestion eradicates Fantom's type inference from the := declaration - something that is heavily used and relied upon.

Fantom's := declaration is quite powerful and has been backed by a lot of thought from day 1, see Topic 3: Def Assign and docLang::Statements#localDef.

Also I suggest to remove const keyword from lang at all.

You seem to be confusing read-only with immutable, which are not the same. For example, given a Buf contains internal mutable state, should this be allowed?

Buf! buf := ...
byte := buf.read

But as Go4 says, the const keyword may be useful for use with local variables.

Login or Signup to reply.