On Wed, Feb 20, 2013, at 08:29 AM, JM wrote:
> 
> There is no such but you can write that interface:
> 
> For example:
> 
> public interface IStringable {
>     public string get_string();
> }
> 

I guess I could write the interface, but what I want to be able to do
is, e.g., simply create a type by passing an int to the generic, then
have the class be able to see if it's type (int in this example). has a
to_string() method.  For example:

public interface IStringable {                                           
    public abstract string to_string();                                  
}                                                                        
                                                                                
      
public class StringableClass : IStringable, Object {                     
    public string to_string() {                                          
        return "foo";                                                    
    }                                                                    
}                                                                        
                                                                                
      
public class NotStringableClass : Object {                               
}                                                                        
                                                                                
      
public class Printer<T> {                                                
    T obj;                                                               
                                                                                
      
    public Printer(T val) {                                              
        obj = val;                                                       
    }                                                                    
                                                                                
      
    public void print() {                                                
        if (obj is IStringable) {                                        
            stdout.printf("%s\n", ((IStringable) obj).to_string());      
        } else {                                                         
            stdout.printf("Not printable.\n");                           
        }                                                                
    }                                                                    
}                                                                        
                                                                                
      
void main() {                                                            
    var i = 10;                                                          
    var yes = new StringableClass();                                     
    var not = new NotStringableClass();                                  
                                                                                
      
    var i_p = new Printer<int>(i);                                       
    var yes_p = new Printer<StringableClass>(yes);                       
    var not_p = new Printer<NotStringableClass>(not);                    
                                                                                
      
    yes_p.print();                                                       
    not_p.print();                
    stdout.print("%s\n", i.to_string());                                 
    i_p.print();                                                         
}                                                                        

valac compiles this fine, but when it gets to i_p.print() it segfaults.

What I want to be able to do is say "yes, I see that int has a
to_string() method" and call it.
_______________________________________________
vala-list mailing list
[email protected]
https://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to