www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - I don't think this is a bug but...

reply Benjamin Shropshire <ao pathlink.com> writes:
The same expression twice gets different results

code:

import std.stdio;

bool Fn(float i){ return true; }
const bool b = Fn(cast(int)0);

static if(b) bool Fn(int i){ return false; }
const bool c = Fn(cast(int)0);

void main()
{
   writef("%s\n", b);
   writef("%s\n", c);
}

output:

true
false
Aug 14 2009
parent reply Jesse Phillips <jessekphillips+d gmail.com> writes:
Benjamin Shropshire Wrote:

 The same expression twice gets different results
 
 code:
 
 import std.stdio;
 
 bool Fn(float i){ return true; }
 const bool b = Fn(cast(int)0);
 
 static if(b) bool Fn(int i){ return false; }
 const bool c = Fn(cast(int)0);
 
 void main()
 {
    writef("%s\n", b);
    writef("%s\n", c);
 }
 
 output:
 
 true
 false
 
The value of 'b' is assigned during compile time, but since it is indirectly called the behavior seems odd. I suppose it is something to be aware of, but it is behaving correctly.
Aug 14 2009
next sibling parent reply BCS <ao pathlink.com> writes:
Reply to Jesse,

 Benjamin Shropshire Wrote:
 
 The same expression twice gets different results
 
 code:
 
 import std.stdio;
 
 bool Fn(float i){ return true; }
 const bool b = Fn(cast(int)0);
 static if(b) bool Fn(int i){ return false; }
 const bool c = Fn(cast(int)0);
 void main()
 {
 writef("%s\n", b);
 writef("%s\n", c);
 }
 output:
 
 true
 false
The value of 'b' is assigned during compile time, but since it is indirectly called the behavior seems odd. I suppose it is something to be aware of, but it is behaving correctly.
The only other option (beside saying this is correct) would be to make it illegal to add a new function to an overload set after the set is used. That could get very tricky to implement.
Aug 14 2009
parent Jeremie Pelletier <jeremiep gmail.com> writes:
BCS Wrote:

 Reply to Jesse,
 
 Benjamin Shropshire Wrote:
 
 The same expression twice gets different results
 
 code:
 
 import std.stdio;
 
 bool Fn(float i){ return true; }
 const bool b = Fn(cast(int)0);
 static if(b) bool Fn(int i){ return false; }
 const bool c = Fn(cast(int)0);
 void main()
 {
 writef("%s\n", b);
 writef("%s\n", c);
 }
 output:
 
 true
 false
The value of 'b' is assigned during compile time, but since it is indirectly called the behavior seems odd. I suppose it is something to be aware of, but it is behaving correctly.
The only other option (beside saying this is correct) would be to make it illegal to add a new function to an overload set after the set is used. That could get very tricky to implement.
I don't think this is a good idea, since overload sets can be hijacked there is nothing that prevents a module from using a local overload set, and another then using it alongside its local hijacks. Having such a "feature" would only limit the programmer in what they can do.
Aug 14 2009
prev sibling parent Jeremie Pelletier <jeremiep gmail.com> writes:
Jesse Phillips Wrote:

 Benjamin Shropshire Wrote:
 
 The same expression twice gets different results
 
 code:
 
 import std.stdio;
 
 bool Fn(float i){ return true; }
 const bool b = Fn(cast(int)0);
 
 static if(b) bool Fn(int i){ return false; }
 const bool c = Fn(cast(int)0);
 
 void main()
 {
    writef("%s\n", b);
    writef("%s\n", c);
 }
 
 output:
 
 true
 false
 
The value of 'b' is assigned during compile time, but since it is indirectly called the behavior seems odd. I suppose it is something to be aware of, but it is behaving correctly.
Exactly, when 'b' is declared, Fn(int) isn't declared yet, as it depends on b being true, int is implicitly convertible to float so it matches the first declaration. When 'c' is declared both functions are available and the second declaration is chosen.
Aug 14 2009