www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - precedence of cast

reply Fawzi Mohamed <fmohamed mac.com> writes:
I was wondering about the precedence of the cast operator.
By experimenting I found that it is less than . and more than +, but I 
was wondering if there isn't a place where this is exactly explained.
A table with the precedence of all operators would be welcome :)

thanks
Fawzi
Aug 27 2008
next sibling parent reply "Manfred_Nowak" <svv1999 hotmail.com> writes:
Fawzi Mohamed wrote:

 A table with the precedence of all operators would be welcome 

If a table is needed, it suggests itself to not rely on yourself and others to know the contents of that table. Use parens and everyone sees the intention. -manfred -- Maybe some knowledge of some types of disagreeing and their relation can turn out to be useful: http://blog.createdebate.com/2008/04/07/writing-strong-arguments/
Aug 27 2008
parent reply Fawzi Mohamed <fmohamed mac.com> writes:
On 2008-08-27 18:14:40 +0200, "Manfred_Nowak" <svv1999 hotmail.com> said:

 Fawzi Mohamed wrote:
 
 A table with the precedence of all operators would be welcome

If a table is needed, it suggests itself to not rely on yourself and others to know the contents of that table. Use parens and everyone sees the intention.

I agree that in general it is a good advice, but if you have an expression that you need to use often, some optimization is in order, because (being it common in your code) excessive parens just weight it down and, whoever looks at your code will have to understand it once, to then apply it in all cases. And anyhow, just for the sake of knowing it I would like someplace where I can look at it. :) Fawzi
 
 -manfred

Aug 27 2008
next sibling parent reply bearophile <bearophileHUGS lycoc.com> writes:
Denis Koroskin:
 That's generally a good idea to have such a table, to reduce number of  
 trial and error, for example.

Having a reference table is positive, but even better is to design a language & programs (D) that usually don't need the programmer (or the person that reads the code) to read such table to understand what the code does. D follows the design of the C language in many things. C has lots of precedence rules, while a language like Ada has chosen to have much less Operator Precedence Levels, to simplify the work of the programmer, see point 4F here: http://www.adahome.com/History/Steelman/steeltab.htm This means that D may eventually choose to decrease its number of precedence levels to simplify it. On the other hand Python has something like 17 precedence levels: http://books.google.it/books?id=nEJ-jcYF2fMC&pg=PA97&lpg=PA97&dq=python+operator+precedence+rules&source=web&ots=3_tES429hg&sig=WiLW5C5JyNXTg1HyJssbM-pe1Ns&hl=en&sa=X&oi=book_result&resnum=1&ct=result#PPA96,M1 Bye, bearophile
Aug 27 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"bearophile" <bearophileHUGS lycoc.com> wrote in message 
news:g94oul$2mi5$1 digitalmars.com...
 Denis Koroskin:
 That's generally a good idea to have such a table, to reduce number of
 trial and error, for example.

Having a reference table is positive, but even better is to design a language & programs (D) that usually don't need the programmer (or the person that reads the code) to read such table to understand what the code does. D follows the design of the C language in many things. C has lots of precedence rules, while a language like Ada has chosen to have much less Operator Precedence Levels, to simplify the work of the programmer, see point 4F here: http://www.adahome.com/History/Steelman/steeltab.htm This means that D may eventually choose to decrease its number of precedence levels to simplify it. On the other hand Python has something like 17 precedence levels: http://books.google.it/books?id=nEJ-jcYF2fMC&pg=PA97&lpg=PA97&dq=python+operator+precedence+rules&source=web&ots=3_tES429hg&sig=WiLW5C5JyNXTg1HyJssbM-pe1Ns&hl=en&sa=X&oi=book_result&resnum=1&ct=result#PPA96,M1 Bye, bearophile

OT: is there anything about D that you _are_ satisfied with?
Aug 27 2008
parent bearophile <bearophileHUGS lycos.com> writes:
Jarrett Billingsley:
 OT: is there anything about D that you _are_ satisfied with? 

There are many things I like of it, but finding and listing them all may require too much time, few of the most important things I like of it: - the people of D community, because with few exceptions, most people seem intelligent, helpful, gentle, etc. - the idea of putting program correctness first, avoiding several mistakes commonly done in C programs - its templates - for having a built-in GC. And I like other things too: - the idea of having built-in AAs and dynamic arrays - its slicing syntax, partially - how numbers can be written, 0b_0000_0000, 1_000_000, etc. - nestable functions - out/ref parameters - Array bounds checking - aliases - the idea of being module-based - static ifs, static asserts - for having a bit of type inference, instead none at all - the idea of having Contract Programming and built-in unit testing - the idea of having built-in ddoc comments That's why I use D when I can, instead of C++ or Cython. But no language is perfect :-) Bye, bearophile
Aug 28 2008
prev sibling parent BCS <ao pathlink.com> writes:
Reply to Fawzi,


 And anyhow, just for the sake of knowing it I would like someplace
 where I can look at it. :)
 

If you know how to read it, the spec is peppered with BNF and that is the authoritative source
Aug 27 2008
prev sibling next sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Wed, 27 Aug 2008 20:32:57 +0400, Fawzi Mohamed <fmohamed mac.com> wrote:

 On 2008-08-27 18:14:40 +0200, "Manfred_Nowak" <svv1999 hotmail.com> said:

 Fawzi Mohamed wrote:

 A table with the precedence of all operators would be welcome

others to know the contents of that table. Use parens and everyone sees the intention.

I agree that in general it is a good advice, but if you have an expression that you need to use often, some optimization is in order, because (being it common in your code) excessive parens just weight it down and, whoever looks at your code will have to understand it once, to then apply it in all cases. And anyhow, just for the sake of knowing it I would like someplace where I can look at it. :) Fawzi
  -manfred


That's generally a good idea to have such a table, to reduce number of trial and error, for example.
Aug 27 2008
prev sibling next sibling parent reply torhu <no spam.invalid> writes:
Fawzi Mohamed wrote:
 I was wondering about the precedence of the cast operator.
 By experimenting I found that it is less than . and more than +, but I 
 was wondering if there isn't a place where this is exactly explained.
 A table with the precedence of all operators would be welcome :)

I just use a C operator precedence table, D follows that: http://www.difranco.net/cop2220/op-prec.htm
Aug 28 2008
parent Fawzi Mohamed <fmohamed mac.com> writes:
On 2008-08-28 18:32:48 +0200, torhu <no spam.invalid> said:

 Fawzi Mohamed wrote:
 I was wondering about the precedence of the cast operator.
 By experimenting I found that it is less than . and more than +, but I 
 was wondering if there isn't a place where this is exactly explained.
 A table with the precedence of all operators would be welcome :)

I just use a C operator precedence table, D follows that: http://www.difranco.net/cop2220/op-prec.htm

thanks, I wasn't sure about cast, but it seems that also that follows the C conversion (even with a different syntax). Fawzi
Aug 29 2008
prev sibling parent Ary Borenszweig <ary esperanto.org.ar> writes:
Fawzi Mohamed wrote:
 I was wondering about the precedence of the cast operator.
 By experimenting I found that it is less than . and more than +, but I 
 was wondering if there isn't a place where this is exactly explained.
 A table with the precedence of all operators would be welcome :)
 
 thanks
 Fawzi
 

If you downloaded DMD, you can take a look at src\dmd\expression.c initPrecedences(). I don't paste it here because I don't know if I'm allowed to do so.
Aug 28 2008