Const keyword

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Const keyword

Simon Richardson
Hi list,

I've seen this mentioned in a few places and looked at the responses of old mailing list threads, but would like to ask the question again (sorry).

I'm wondering if we could take advantage of the const keyword for coding. I know Nicholas is keen to keep everything open once it passes through the compiler (hence all methods being public), but one thing I really like in languages is the ability to have immutable properties or local variables. We could just use the compiler to check the values are immutable, and then just output them as normal variables for all platform implementations (unless there is a speed update, in flash I don't think it does much, although there is some debate about this?). I personally think this helps people whom read my code about what is reusable and what isn't, also it helps me understand the state of a class. I personally think this is a bit more than syntax sugar, but that's my opinion ;-)

Also could @:final do anything here, if so I would be more than happy for that!

With haXe getting more traction (see mailing list getting more active again) I wouldn't be surprised if this question gets ask again.

Thanks
simon
--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Const keyword

Baluta Cristian
Don't know if you know or you just want the "const" keyword, but in haxe is something better than const, it's "inline static var". You can't change it's value at runtime, and is in fact replaced in the code at compile time, which makes the code faster.
So, what are the advantages of const?


On Mon, Sep 6, 2010 at 3:03 PM, Simon Richardson <[hidden email]> wrote:
Hi list,

I've seen this mentioned in a few places and looked at the responses of old mailing list threads, but would like to ask the question again (sorry).

I'm wondering if we could take advantage of the const keyword for coding. I know Nicholas is keen to keep everything open once it passes through the compiler (hence all methods being public), but one thing I really like in languages is the ability to have immutable properties or local variables. We could just use the compiler to check the values are immutable, and then just output them as normal variables for all platform implementations (unless there is a speed update, in flash I don't think it does much, although there is some debate about this?). I personally think this helps people whom read my code about what is reusable and what isn't, also it helps me understand the state of a class. I personally think this is a bit more than syntax sugar, but that's my opinion ;-)

Also could @:final do anything here, if so I would be more than happy for that!

With haXe getting more traction (see mailing list getting more active again) I wouldn't be surprised if this question gets ask again.

Thanks
simon
--
haXe - an open source web programming language
http://haxe.org



--
Băluță Cristian
http://ralcr.com
http://imagin.ro

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Const keyword

MarcWeber
Excerpts from Baluta Cristian's message of Mon Sep 06 14:14:49 +0200 2010:
> Don't know if you know or you just want the "const" keyword, but in haxe is
> something better than const, it's "inline static var". You can't change it's
> value at runtime, and is in fact replaced in the code at compile time, which
> makes the code faster.
> So, what are the advantages of const?

I think the question was about this use case:

function(){
  const var x = "I konw I'm not going to touch this value / reference again";
  x = 27; // failure
  const var obj = new Object();
  obj.add("foo"); // but I can still change the state of objects.
}

In Java its called final.

Marc Weber

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Const keyword

Simon Richardson
In reply to this post by Baluta Cristian

On 6 Sep 2010, at 13:14, Baluta Cristian wrote:

Don't know if you know or you just want the "const" keyword, but in haxe is something better than const, it's "inline static var". You can't change it's value at runtime, and is in fact replaced in the code at compile time, which makes the code faster.

You mean this -> http://haxe.org/ref/inline - I did know about that, but I'm wondering how the code can be more concise and to the point? 
(I do realise this is syntax sugar, but it does for me at least improve readability).

So, what are the advantages of const?

package;
class Something<T>
{
private var array : Array<T>;

public function new() 
{
array = new Array<T>();
}

public function doThis() : Void
{
const num = array.length; // immutable value!
num = 2; // error - can not change value.
}
}

concise and to the point, improves readability of code and considering code bases for applications are getting big improving readability is a big win for me.




On Mon, Sep 6, 2010 at 3:03 PM, Simon Richardson <[hidden email]> wrote:
Hi list,

I've seen this mentioned in a few places and looked at the responses of old mailing list threads, but would like to ask the question again (sorry).

I'm wondering if we could take advantage of the const keyword for coding. I know Nicholas is keen to keep everything open once it passes through the compiler (hence all methods being public), but one thing I really like in languages is the ability to have immutable properties or local variables. We could just use the compiler to check the values are immutable, and then just output them as normal variables for all platform implementations (unless there is a speed update, in flash I don't think it does much, although there is some debate about this?). I personally think this helps people whom read my code about what is reusable and what isn't, also it helps me understand the state of a class. I personally think this is a bit more than syntax sugar, but that's my opinion ;-)

Also could @:final do anything here, if so I would be more than happy for that!

With haXe getting more traction (see mailing list getting more active again) I wouldn't be surprised if this question gets ask again.

Thanks
simon
--
haXe - an open source web programming language
http://haxe.org



--
Băluță Cristian
http://ralcr.com
http://imagin.ro
--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Const keyword

Simon Richardson
In reply to this post by MarcWeber
exactly.

On 6 Sep 2010, at 13:31, Marc Weber wrote:

> Excerpts from Baluta Cristian's message of Mon Sep 06 14:14:49 +0200 2010:
>> Don't know if you know or you just want the "const" keyword, but in haxe is
>> something better than const, it's "inline static var". You can't change it's
>> value at runtime, and is in fact replaced in the code at compile time, which
>> makes the code faster.
>> So, what are the advantages of const?
>
> I think the question was about this use case:
>
> function(){
>  const var x = "I konw I'm not going to touch this value / reference again";
>  x = 27; // failure
>  const var obj = new Object();
>  obj.add("foo"); // but I can still change the state of objects.
> }
>
> In Java its called final.
>
> Marc Weber
>
> --
> haXe - an open source web programming language
> http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Const keyword

Serhat Sakarya
In reply to this post by Simon Richardson
For this scenario, 'const' seems a lot more intuitive than 'inline'.

My general expectation for the inline keyword would be that it only affects performance and generated code.

Regards,

Serhat

On Mon, Sep 6, 2010 at 2:32 PM, Simon Richardson <[hidden email]> wrote:
On 6 Sep 2010, at 13:14, Baluta Cristian wrote:

concise and to the point, improves readability of code and considering code bases for applications are getting big improving readability is a big win for me.


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Const keyword

Raoul Duke
In reply to this post by MarcWeber
On Mon, Sep 6, 2010 at 5:31 AM, Marc Weber <[hidden email]> wrote:
> function(){
>  const var x = "I konw I'm not going to touch this value / reference again";
>  x = 27; // failure
>  const var obj = new Object();
>  obj.add("foo"); // but I can still change the state of objects.
> }
>
> In Java its called final.

i think this points out the whole can of worms that is Java's "final".
in my humble opinion, it doesn't go far enough because it only makes
sure the reference cannot change, but the referant can change if it is
not inherently immutable (Java Strings are immutable).

sincerely.

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Const keyword

Simon Richardson

On 7 Sep 2010, at 18:25, Raoul Duke wrote:

> On Mon, Sep 6, 2010 at 5:31 AM, Marc Weber <[hidden email]> wrote:
>> function(){
>>  const var x = "I konw I'm not going to touch this value / reference again";
>>  x = 27; // failure
>>  const var obj = new Object();
>>  obj.add("foo"); // but I can still change the state of objects.
>> }
>>
>> In Java its called final.
>
> i think this points out the whole can of worms that is Java's "final".

that's the same in a lot of languages not just Java (C# for one)!

> in my humble opinion, it doesn't go far enough because it only makes
> sure the reference cannot change, but the referant can change if it is
> not inherently immutable (Java Strings are immutable).
>
> sincerely.
>
> --
> haXe - an open source web programming language
> http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Const keyword

Raoul Duke
On Tue, Sep 7, 2010 at 11:02 AM, Simon Richardson <[hidden email]> wrote:
> that's the same in a lot of languages not just Java (C# for one)!

if everybody jumps off of the eiffel tower...?

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Const keyword

Nicolas Cannasse
In reply to this post by Simon Richardson
Le 06/09/2010 14:03, Simon Richardson a écrit :
> Hi list,
>
> I've seen this mentioned in a few places and looked at the responses of old mailing list threads, but would like to ask the question again (sorry).
>
> I'm wondering if we could take advantage of the const keyword for coding. I know Nicholas is keen to keep everything open once it passes through the compiler (hence all methods being public), but one thing I really like in languages is the ability to have immutable properties or local variables. We could just use the compiler to check the values are immutable, and then just output them as normal variables for all platform implementations (unless there is a speed update, in flash I don't think it does much, although there is some debate about this?). I personally think this helps people whom read my code about what is reusable and what isn't, also it helps me understand the state of a class. I personally think this is a bit more than syntax sugar, but that's my opinion ;-)

Immutable properties can be already defined through "static inline var".
Immutable variables can be easily detected by the compiler, no need for
the developer to tag them ;)

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Const keyword

Pimm Hogeling
In reply to this post by Raoul Duke
I would like to add that I expect the behaviour Raoul mentions. Similar to the way most immutable list/array implementations work: you cannot mutate the collection, but you might still be able to mutate the values inside.

On 7 September 2010 19:25, Raoul Duke <[hidden email]> wrote:
On Mon, Sep 6, 2010 at 5:31 AM, Marc Weber <[hidden email]> wrote:
> function(){
>  const var x = "I konw I'm not going to touch this value / reference again";
>  x = 27; // failure
>  const var obj = new Object();
>  obj.add("foo"); // but I can still change the state of objects.
> }
>
> In Java its called final.

i think this points out the whole can of worms that is Java's "final".
in my humble opinion, it doesn't go far enough because it only makes
sure the reference cannot change, but the referant can change if it is
not inherently immutable (Java Strings are immutable).

sincerely.

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
O-2
Reply | Threaded
Open this post in threaded view
|

Re: Const keyword

O-2
In reply to this post by Simon Richardson
  06.09.2010 16:34, Simon Richardson пишет:

> exactly.
>
> On 6 Sep 2010, at 13:31, Marc Weber wrote:
>> I think the question was about this use case:
>> function(){
>>   const var x = "I konw I'm not going to touch this value / reference again";
>>   x = 27; // failure
>>   const var obj = new Object();
>>   obj.add("foo"); // but I can still change the state of objects.
>> }
>>
May I ask, what is the practical usage of this feature?

As far as I understand at present moment, you can get an immutable
property of a class by using:
class Foo{
public var someImmutableVar(getTheImmutableVar, never);

private getTheImmutableVar(){...}
}

For local variables... Local variables are all under your own control.
So just don't do things you don't want to be done - don't change local
variables =)

--
haXe - an open source web programming language
http://haxe.org