#2773 Compiling Const Lists and Maps

SlimerDude Sat 5 Oct 2019

const class Example {
    const NonConstClass[] oops  // this line compiles quite happily!
	
    new make(|This| f) { f(this) }
}

class NonConstClass { }

I notice the Fantom compiler lets you define const fields of Lists and Maps with non-const types.

I understand that the actual type is sys::List or sys::Map which may or may not be immutable, but I'd have thought the compiler would be able to inspect the defined parametrised type to make a judgement call.

Could this not be added?

brian Mon 2 Mar

We allow it because you are allowed to have an const field for an empty list or empty map of any type. For example:

fansh> Buf[,].toImmutable.isImmutable
true

So the rule is that list and map types are checked at runtime by calling toImmutable on them.

Login or Signup to reply.