www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How about 'pure' for constants?

reply Don Clugston <dac nospam.com.au> writes:
IIRC, there was a suggestion at the conference that D would get a new reserved 
word, 'pure', for functional programming. This should be considered as an 
alternative to 'enum'. I really don't like the idea of

enum SomeEnormousStruct a = SomeFunction(AnotherEnormousStruct(x, "abc"));

(and my CTFE code is already full of this sort of thing. It's simply not true 
that 'enums' would mostly be integral types).

Seems to fit with the idea of 'having no side-effects' - a pure value would not 
be stored anywhere, and make no contribution to the size of the executable.

pure real pi = 3.141592564;

// this is really silly if you use 'enum' instead.
pure real myNaN = real.nan;

BTW, a pure function taking only pure parameters returns a pure value, so this 
seems to be entirely consistent:

pure int foo(int a, int b);
Dec 11 2007
next sibling parent "Aziz K." <aziz.kerim gmail.com> writes:
Introducing 'pure' as a new storage class for denoting manifest constants  
and functions without sideeffects sounds very good to me. I'd be happy  
with that solution. Much better than abusing the poor enum keyword. It  
just wants to enumerate stuff, can't you see it? Let's not be that cruel :)
Dec 11 2007
prev sibling next sibling parent Daniel Keep <daniel.keep.lists gmail.com> writes:
This is by far the best suggestion thus far.  This way, there's no
confusion over what it does based on existing behaviour, we can define
exactly how it should behave, and it's logically consistent with pure
functions.

Nicely done :)

	-- Daniel
Dec 11 2007
prev sibling next sibling parent Sean Kelly <sean f4.ca> writes:
Don Clugston wrote:
 IIRC, there was a suggestion at the conference that D would get a new 
 reserved word, 'pure', for functional programming. This should be 
 considered as an alternative to 'enum'. I really don't like the idea of
 
 enum SomeEnormousStruct a = SomeFunction(AnotherEnormousStruct(x, "abc"));
 
 (and my CTFE code is already full of this sort of thing. It's simply not 
 true that 'enums' would mostly be integral types).
 
 Seems to fit with the idea of 'having no side-effects' - a pure value 
 would not be stored anywhere, and make no contribution to the size of 
 the executable.
 
 pure real pi = 3.141592564;
 
 // this is really silly if you use 'enum' instead.
 pure real myNaN = real.nan;
 
 BTW, a pure function taking only pure parameters returns a pure value, 
 so this seems to be entirely consistent:
 
 pure int foo(int a, int b);

I've been wondering the same thing, but couldn't decide if the keyword made sense in this context. But by your explanation it clearly does. It has my vote. Sean
Dec 11 2007
prev sibling next sibling parent reply "Craig Black" <cblack ara.com> writes:
"Don Clugston" <dac nospam.com.au> wrote in message 
news:fjli16$1efd$1 digitalmars.com...
 IIRC, there was a suggestion at the conference that D would get a new 
 reserved word, 'pure', for functional programming. This should be 
 considered as an alternative to 'enum'. I really don't like the idea of

 enum SomeEnormousStruct a = SomeFunction(AnotherEnormousStruct(x, "abc"));

 (and my CTFE code is already full of this sort of thing. It's simply not 
 true that 'enums' would mostly be integral types).

 Seems to fit with the idea of 'having no side-effects' - a pure value 
 would not be stored anywhere, and make no contribution to the size of the 
 executable.

 pure real pi = 3.141592564;

 // this is really silly if you use 'enum' instead.
 pure real myNaN = real.nan;

 BTW, a pure function taking only pure parameters returns a pure value, so 
 this seems to be entirely consistent:

 pure int foo(int a, int b);

Much better than enum. Thanks Don.
Dec 11 2007
parent reply "Janice Caron" <caron800 googlemail.com> writes:
Best suggestion so far. (Even better than wildebeest! :-) )

Now let's see what Walter and Andrei think.
Dec 11 2007
parent =?ISO-8859-1?Q?=22J=E9r=F4me_M=2E_Berger=22?= <jeberger free.fr> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Janice Caron wrote:
 Best suggestion so far. (Even better than wildebeest! :-) )
 

Jerome - -- +------------------------- Jerome M. BERGER ---------------------+ | mailto:jeberger free.fr | ICQ: 238062172 | | http://jeberger.free.fr/ | Jabber: jeberger jabber.fr | +---------------------------------+------------------------------+ -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQFHXt1Yd0kWM4JG3k8RAu8xAJ9ylvOBvx4Gj5c7GMgogC0Koyv5TgCglPvn WRHpTlXo/mToR4ccWMgvsAI= =eNDA -----END PGP SIGNATURE-----
Dec 11 2007
prev sibling next sibling parent reply Robert Fraser <fraserofthenight gmail.com> writes:
Don Clugston wrote:
 IIRC, there was a suggestion at the conference that D would get a new 
 reserved word, 'pure', for functional programming. This should be 
 considered as an alternative to 'enum'. I really don't like the idea of
 
 enum SomeEnormousStruct a = SomeFunction(AnotherEnormousStruct(x, "abc"));
 
 (and my CTFE code is already full of this sort of thing. It's simply not 
 true that 'enums' would mostly be integral types).
 
 Seems to fit with the idea of 'having no side-effects' - a pure value 
 would not be stored anywhere, and make no contribution to the size of 
 the executable.
 
 pure real pi = 3.141592564;
 
 // this is really silly if you use 'enum' instead.
 pure real myNaN = real.nan;
 
 BTW, a pure function taking only pure parameters returns a pure value, 
 so this seems to be entirely consistent:
 
 pure int foo(int a, int b);

Hmmm... It's not a bad idea, but (like with enum & final), this worries me: pure { int x = 5; // Manifest Constant void func() { } // Function without side effects }
Dec 11 2007
next sibling parent "Janice Caron" <caron800 googlemail.com> writes:
On 12/11/07, Robert Fraser <fraserofthenight gmail.com> wrote:
 Hmmm... It's not a bad idea, but (like with enum & final), this worries me:

 pure
 {
      int x = 5;      // Manifest Constant
      void func() { } // Function without side effects
 }

That's not really worrying at all. Think of it like this pure int x() = 5; A function without side-effects. And since you can't take the address, what's the difference?
Dec 11 2007
prev sibling parent "Janice Caron" <caron800 googlemail.com> writes:
On Dec 11, 2007 10:05 PM, Janice Caron <caron800 googlemail.com> wrote:
 On 12/11/07, Robert Fraser <fraserofthenight gmail.com> wrote:
 Hmmm... It's not a bad idea, but (like with enum & final), this worries me:

 pure
 {
      int x = 5;      // Manifest Constant
      void func() { } // Function without side effects
 }

That's not really worrying at all. Think of it like this pure int x() = 5;

Guess I should have written pure int x() { return 5; } but you get the idea. If you're not allowed to take the address, then x the function behaves exactly like x the constant. That being so, I see no problem with using the same keyword for both. It seems appropriate somehow. In both cases, there are no side-effects.
Dec 11 2007
prev sibling next sibling parent Tom <tom nospam.com> writes:
Don Clugston escribió:
 IIRC, there was a suggestion at the conference that D would get a new 
 reserved word, 'pure', for functional programming. This should be 
 considered as an alternative to 'enum'.
 [clipped]

myVote++; -- Tom;
Dec 11 2007
prev sibling next sibling parent Alexander Panek <alexander.panek brainsware.org> writes:
On Tue, 11 Dec 2007 09:33:40 +0100
Don Clugston <dac nospam.com.au> wrote:

 Seems to fit with the idea of 'having no side-effects' - a pure value
 would not be stored anywhere, and make no contribution to the size of
 the executable.
 
 pure real pi = 3.141592564;
 
 // this is really silly if you use 'enum' instead.
 pure real myNaN = real.nan;
 
 BTW, a pure function taking only pure parameters returns a pure
 value, so this seems to be entirely consistent:
 
 pure int foo(int a, int b);

I like that very much. -- Alexander Panek <alexander.panek brainsware.org>
Dec 12 2007
prev sibling next sibling parent Russell Lewis <webmaster villagersonline.com> writes:
vote++
Dec 12 2007
prev sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Don Clugston" <dac nospam.com.au> wrote in message 
news:fjli16$1efd$1 digitalmars.com...
 IIRC, there was a suggestion at the conference that D would get a new 
 reserved word, 'pure', for functional programming. This should be 
 considered as an alternative to 'enum'. I really don't like the idea of

 enum SomeEnormousStruct a = SomeFunction(AnotherEnormousStruct(x, "abc"));

 (and my CTFE code is already full of this sort of thing. It's simply not 
 true that 'enums' would mostly be integral types).

 Seems to fit with the idea of 'having no side-effects' - a pure value 
 would not be stored anywhere, and make no contribution to the size of the 
 executable.

 pure real pi = 3.141592564;

 // this is really silly if you use 'enum' instead.
 pure real myNaN = real.nan;

 BTW, a pure function taking only pure parameters returns a pure value, so 
 this seems to be entirely consistent:

 pure int foo(int a, int b);

Votity vote vote vote!
Dec 12 2007