コード例 #1
0
            public T To <T> () where T : struct
            {
                BaseTerm e = ChainEnd();

                try
                {
                    return((e is ValueTerm)
            ? (T)Convert.ChangeType(e.functor, typeof(T))
            : e.Eval().To <T> ());
                }
                catch
                {
                    if (e is NamedVariable)
                    {
                        IO.Error("Unable to convert unbound variable {0} to type {1}",
                                 ((NamedVariable)e).Name, typeof(T).UnderlyingSystemType.Name);
                    }
                    else if (e is Variable)
                    {
                        IO.Error("Unable to convert an unbound variable to type {0}",
                                 typeof(T).UnderlyingSystemType.Name);
                    }
                    else if (e is ListTerm)
                    {
                        IO.Error("Unable to convert list {0} to type {1}",
                                 e, typeof(T).UnderlyingSystemType.Name);
                    }
                    else
                    {
                        IO.Error("Unable to convert '{0}' to type {1}",
                                 e.FunctorToString, typeof(T).UnderlyingSystemType.Name);
                    }

                    return(default(T)); // IO.Error throws error, but compiler insists on a return value
                }
            }