Problem with type cast in if condition

NiRu via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Oct 6 13:35:26 PDT 2015


Hi there,

I have a problem with the typecast within my if condition, the 
dmd2 compiler says he can not cast int to User, but it never 
reaches an int type the condition (at most, the else branch).

my try:

struct User {
	string name;
}

void printName(T)(T value)
{
	if(is(typeof(value) == User)){
		User u = cast(User) value;
		writeln(u.name);
		
		write("User Type: ");
		writeln(value);
	} else {
		write("Another Type: ");
		writeln(value);
	}
	
}

void main(string[] args)
{
	User person;
	person.name = "Jacub";
	
	printName(362);
	printName("have a nice day");
	printName(person);
}



without the type cast line I have the following output:


Another Type: 362
Another Type: have a nice day
User Type: User("Jacub")


Instead with type cast "Error: cannot cast expression value of 
type int to User"

where is the problem?

Thanks!


More information about the Digitalmars-d-learn mailing list