www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Am I doing it wrong?

reply Emil Madsen <sovende gmail.com> writes:
So I wrote a program, to find prime numbers, just to check out this pure
thing;

http://gist.github.com/608493

However, the program has a runtime of about 5 seconds? - in my mind, if the
function is pure, shouldn't the compiler insure that it was evaluated at
compiletime? - or am I doing it wrong?

-- 
// Yours sincerely
// Emil 'Skeen' Madsen
Oct 03 2010
next sibling parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Sun, 03 Oct 2010 14:54:06 +0400, Emil Madsen <sovende gmail.com> wrote:

 So I wrote a program, to find prime numbers, just to check out this pure
 thing;

 http://gist.github.com/608493

 However, the program has a runtime of about 5 seconds? - in my mind, if  
 the
 function is pure, shouldn't the compiler insure that it was evaluated at
 compiletime? - or am I doing it wrong?
Make your result an "enum" (i.e. compile-time constant) if you really want to calculate it in compile-time: enum primes = calcPrimes();
Oct 03 2010
next sibling parent reply Emil Madsen <sovende gmail.com> writes:
Well the result is assigned to an immutable int, shouldn't that be a compile
const too?

2010/10/3 Denis Koroskin <2korden gmail.com>

 On Sun, 03 Oct 2010 14:54:06 +0400, Emil Madsen <sovende gmail.com> wrote:

  So I wrote a program, to find prime numbers, just to check out this pure
 thing;

 http://gist.github.com/608493

 However, the program has a runtime of about 5 seconds? - in my mind, if
 the
 function is pure, shouldn't the compiler insure that it was evaluated at
 compiletime? - or am I doing it wrong?
Make your result an "enum" (i.e. compile-time constant) if you really want to calculate it in compile-time: enum primes = calcPrimes();
-- // Yours sincerely // Emil 'Skeen' Madsen
Oct 03 2010
parent "Denis Koroskin" <2korden gmail.com> writes:
On Sun, 03 Oct 2010 15:08:33 +0400, Emil Madsen <sovende gmail.com> wrote:

 Well the result is assigned to an immutable int, shouldn't that be a  
 compile
 const too?
No
Oct 03 2010
prev sibling next sibling parent Torarin <torarind gmail.com> writes:
2010/10/3 Emil Madsen <sovende gmail.com>:
 Well the result is assigned to an immutable int, shouldn't that be a compile
 const too?
Immutable means that the variable, or the memory it points to, will not change. You can still assign run-time values to it: void main(string[] args) { immutable string a = args[0]; writeln(a); }
Oct 03 2010
prev sibling next sibling parent reply Emil Madsen <sovende gmail.com> writes:
ah ofc! I shoulda know :) - So I were doing it wrong :)
Say I'm doing that enum a = calcPrimes();
then a will be an enum with 1 element, that I can use as an int right?
- or is there something special to be aware of?

On 3 October 2010 13:20, Torarin <torarind gmail.com> wrote:

 2010/10/3 Emil Madsen <sovende gmail.com>:
 Well the result is assigned to an immutable int, shouldn't that be a
compile
 const too?
Immutable means that the variable, or the memory it points to, will not change. You can still assign run-time values to it: void main(string[] args) { immutable string a = args[0]; writeln(a); }
-- // Yours sincerely // Emil 'Skeen' Madsen
Oct 03 2010
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
Emil Madsen wrote:
 ah ofc! I shoulda know :) - So I were doing it wrong :)
Thanks for the question; I learned something too. :) But in case you don't already know, there is also the D.learn newsgroup. This question might be more useful there. Ali
 Say I'm doing that enum a = calcPrimes();
 then a will be an enum with 1 element, that I can use as an int right?
 - or is there something special to be aware of?
 
 On 3 October 2010 13:20, Torarin <torarind gmail.com> wrote:
 
 2010/10/3 Emil Madsen <sovende gmail.com>:
 Well the result is assigned to an immutable int, shouldn't that be a
compile
 const too?
Immutable means that the variable, or the memory it points to, will not change. You can still assign run-time values to it: void main(string[] args) { immutable string a = args[0]; writeln(a); }
Oct 03 2010
parent Emil Madsen <sovende gmail.com> writes:
So I've been told, but havn't been able to find it, so I rechecked, and
there it was, apperently I'm getting blind >.<
I'll make sure not to pollute this mailing list with these questions again
then :) - Thanks once again

On 3 October 2010 23:28, Ali =C7ehreli <acehreli yahoo.com> wrote:

 Emil Madsen wrote:

 ah ofc! I shoulda know :) - So I were doing it wrong :)
Thanks for the question; I learned something too. :) But in case you don't already know, there is also the D.learn newsgroup. This question might be more useful there. Ali Say I'm doing that enum a =3D calcPrimes();
 then a will be an enum with 1 element, that I can use as an int right?
 - or is there something special to be aware of?

 On 3 October 2010 13:20, Torarin <torarind gmail.com> wrote:

  2010/10/3 Emil Madsen <sovende gmail.com>:
 Well the result is assigned to an immutable int, shouldn't that be a
compile
 const too?
Immutable means that the variable, or the memory it points to, will not change. You can still assign run-time values to it: void main(string[] args) { immutable string a =3D args[0]; writeln(a); }
--=20 // Yours sincerely // Emil 'Skeen' Madsen
Oct 03 2010
prev sibling next sibling parent Torarin <torarind gmail.com> writes:
2010/10/3 Emil Madsen <sovende gmail.com>:
 ah ofc! I shoulda know :) - So I were doing it wrong :)
 Say I'm doing that enum a = calcPrimes();
 then a will be an enum with 1 element, that I can use as an int right?
Yes, effectively you are declaring an anonymous enum with one element. By default an int.
Oct 03 2010
prev sibling parent reply Emil Madsen <sovende gmail.com> writes:
can the enum be a float? if calcprimes returned a float? - and if so, will
the enum be a float or an int? (will it be casted, or will it work as an
auto type?)

thanks btw :)

On 3 October 2010 13:28, Torarin <torarind gmail.com> wrote:

 2010/10/3 Emil Madsen <sovende gmail.com>:
 ah ofc! I shoulda know :) - So I were doing it wrong :)
 Say I'm doing that enum a = calcPrimes();
 then a will be an enum with 1 element, that I can use as an int right?
Yes, effectively you are declaring an anonymous enum with one element. By default an int.
-- // Yours sincerely // Emil 'Skeen' Madsen
Oct 03 2010
parent reply =?UTF-8?B?IkrDqXLDtG1lIE0uIEJlcmdlciI=?= <jeberger free.fr> writes:
Emil Madsen wrote:
 can the enum be a float? if calcprimes returned a float? - and if so, w=
ill
 the enum be a float or an int? (will it be casted, or will it work as a=
n
 auto type?)
 =20
Yes, basically "enum" is a synonym for "compile-time const auto" (although the "auto" part can be replaced by an explicit type). Jerome --=20 mailto:jeberger free.fr http://jeberger.free.fr Jabber: jeberger jabber.fr
Oct 03 2010
parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
J=C3=A9r=C3=B4me M. Berger <jeberger free.fr> wrote:

 Emil Madsen wrote:
 can the enum be a float? if calcprimes returned a float? - and if so,=
=
 will
 the enum be a float or an int? (will it be casted, or will it work as=
an
 auto type?)
Yes, basically "enum" is a synonym for "compile-time const auto" (although the "auto" part can be replaced by an explicit type).
Or, you know, just "compile-time const", as the lack of a specified type= , and const being a storage class, allows for type inference. Auto is not necessary. -- = Simen
Oct 03 2010
parent =?UTF-8?B?IkrDqXLDtG1lIE0uIEJlcmdlciI=?= <jeberger free.fr> writes:
Simen kjaeraas wrote:
 J=C3=A9r=C3=B4me M. Berger <jeberger free.fr> wrote:
=20
 Emil Madsen wrote:
 can the enum be a float? if calcprimes returned a float? - and if so,=
 will
 the enum be a float or an int? (will it be casted, or will it work as=
an
 auto type?)
Yes, basically "enum" is a synonym for "compile-time const auto" (although the "auto" part can be replaced by an explicit type).
=20 Or, you know, just "compile-time const", as the lack of a specified typ=
e,
 and const being a storage class, allows for type inference. Auto is not=
 necessary.
=20
I know, but since the question was "can the enum be a float?", I felt it necessary to put the redundant "auto" in my explanation. Jerome --=20 mailto:jeberger free.fr http://jeberger.free.fr Jabber: jeberger jabber.fr
Oct 04 2010
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday 03 October 2010 04:34:31 Emil Madsen wrote:
 can the enum be a float? if calcprimes returned a float? - and if so, will
 the enum be a float or an int? (will it be casted, or will it work as an
 auto type?)
auto, enum, immutable, and const all use type inference. So, you can declare auto a = 7; enum b = 7.7; immutable c = "hello"; const d = false; enum, however, is the only one which is a compile-time constant. - Jonathan M Davis
Oct 03 2010
prev sibling parent Emil Madsen <sovende gmail.com> writes:
perfect :), thanks :)

On 3 October 2010 13:39, Jonathan M Davis <jmdavisProg gmx.com> wrote:

 On Sunday 03 October 2010 04:34:31 Emil Madsen wrote:
 can the enum be a float? if calcprimes returned a float? - and if so,
will
 the enum be a float or an int? (will it be casted, or will it work as an
 auto type?)
auto, enum, immutable, and const all use type inference. So, you can declare auto a = 7; enum b = 7.7; immutable c = "hello"; const d = false; enum, however, is the only one which is a compile-time constant. - Jonathan M Davis
-- // Yours sincerely // Emil 'Skeen' Madsen
Oct 03 2010