Thought for a write-once constant class

Another conversation lead me to this thought: A class that works like a write-once string constant (I need a better name for this).

The idea is that you want to create a constant, but won’t know what the value will be until runtime, OR you know what the value is, but it’s not something you can easily assign with the const keyword.

My idea is a class that takes a value in its constructor, then return it with Operator_Convert, but there will be no way to change the value once assigned other than assign another instance of the class. You’d use it like this:

dim kMyString as new WriteOnceConstant( ChrB( 255 ) + ChrB( 254 ) )
dim s as string = kMyString // Goes through Operator_Convert
kMyString = s // Wouldn't compile

Add a boolean IsSet to the class and check for it?

Can’t see why that would be needed. You’d set it when it’s created.

And how do you prevent it being set again?

Ah - missed the constructor.

There would be no Operator_Convert from a string, but nothing would stop you from assigning it a new instance like this:

dim k as new WriteOnceConstant( "some value" )
k = "some other value" // Won't compile
k = new WriteOnceConstant( "some other value" ) // Well, why did you go and do that?!?

I can’t think of a way around that.

[quote=50862:@Kem Tekinay]Another conversation lead me to this thought: A class that works like a write-once string constant (I need a better name for this).
[/quote]
Its called APL :stuck_out_tongue:

[quote=50862:@Kem Tekinay]The idea is that you want to create a constant, but won’t know what the value will be until runtime, OR you know what the value is, but it’s not something you can easily assign with the const keyword.

My idea is a class that takes a value in its constructor, then return it with Operator_Convert, but there will be no way to change the value once assigned other than assign another instance of the class. You’d use it like this:

dim kMyString as new WriteOnceConstant( ChrB( 255 ) + ChrB( 254 ) ) dim s as string = kMyString // Goes through Operator_Convert kMyString = s // Wouldn't compile [/quote]

Umm this is a great place for a computed property
You can limit it to one write the read it as many times as you want

Next

Yes, you certainly could do it that way, but that’s not exactly portable, and you can’t do it inline as you can with the const keyword. Once you have the class, you can throw it into code without much fuss.

Or you could just dismiss the idea out of hand…

I would think that a global computed property with an IsSet flag in the setter to limit it to one time only would be the way to go.

Just trying to point out there is a way to do this with things already at your dispel without having to resort to changing the language

Or you could just dismiss the idea out of hand…

Sorry, I wasn’t talking about changing the language. This isn’t a feature request, it’s an idea for something we can implement ourselves with the tools we have.

C++ kind of has this idea
You can declare a member as const and it can only be set in the initializer list

But with the language you have you can do mostly the same thing with a write once computed property etc
It’s by convention as opposed to enforced by the compiler but ……

With a computed property, you have to create that property, it’s shadow property, and a shadow “isSet” property for each of these, no? (You’d need the “isSet” property in case the thing is set to an empty string.)

I’d prefer to encapsulate this in a reusable class. But maybe that’s just me. :slight_smile:

The IsSet property can be a static local variable in the Setter. The problem with making it a class is that it isn’t as “constant” in that you can create a new instance and replace the original.

Agreed, that’s a drawback, but presumably you won’t do that.

If you using static, you will only be able to use it once throughout the entire project for that particular property, even if different instances should have different values.

That is true of class members, but not in a module.

What about a Module that has a global method, say “DynamicConstant” that returns a class instance. That class has an operator_lookup that returns a string and one that assigns a string. It stores those values in a dictionary unless the dictionary already has that key.

Then you can write

DynamicConstant.someValue=“The value of somevalue”
(subsequent assignments fail silently or could raise an exception if needed)
and later…
someOtherValue=DynamicConstant.someValue

If the key is not found you could either return “” or raise an exception.
Global scope… although you could create a new instance of the class in a class or module or in code using static.
myDynConst=new mDynamicConstant

I haven’t tried, will Operator_Lookup work with a module?

But beyond that, I don’t like the idea of a Module only because it’s global. Again, if two instances of a class should have a different value for the same constant, this wouldn’t work.

Having said that, I like your idea… a lot. If it’s a class that you have to instantiate, you get more flexibility. You can store the instance in a global module property or within a window or class.

Operator_Lookup won’t work in a module… that’s why the module would have to have a method that returns an instance of a class to have global scope. But you could have a module with a GlobalDynamicConstant method and local instances of the DynamicConstant class at the same time.

[quote=50926:@Kem Tekinay]
Having said that, I like your idea… a lot. If it’s a class that you have to instantiate, you get more flexibility. You can store the instance in a global module property or within a window or class.[/quote]

But there’s still nothing to stop you doing

myWriteOnceConstant = new WriteOnceConstant( some value )

which kind of makes it “not constant” :stuck_out_tongue: