예제 #1
0
        /// <summary>
        /// Converts the term to a constructor with the specified name and arity.
        /// </summary>
        /// <param name="term">The term to convert.</param>
        /// <param name="name">The expected name.</param>
        /// <param name="arity">The expected arity.</param>
        /// <returns>The resulting value.</returns>
        /// <exception cref="InvalidCastException">
        /// The term could not be converted.
        /// </exception>
        public static IConsTerm ToCons(this ITerm term, string name, int arity)
        {
            #region Contract
            if (term == null)
            {
                throw new ArgumentNullException(nameof(term));
            }
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (arity < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(arity));
            }
            #endregion

            var result = term.AsCons(name, arity);
            if (result == null)
            {
                throw new InvalidCastException($"Can't cast term {term} to {name}`{arity}.");
            }
            return(result);
        }