TypeError(func::Symbol, context::AbstractString, expected::Type, got)

A type assertion failure, or calling an intrinsic function with an incorrect argument type.


In the Julia programming language, the function TypeError(func::Symbol, context::AbstractString, expected::Type, got) is used to generate a type assertion failure error message or an error message when calling an intrinsic function with an incorrect argument type.

Here are some examples of how to use the TypeError function:

julia> TypeError(:foo, "bar", Int, Float64)
ERROR: MethodError: no method matching foo(::Float64)
Closest candidates are:
  foo(::Int64) at REPL[1]:1

In this example, a TypeError is generated with the function name foo, context "bar", expected type Int, and actual type Float64. The error message indicates that there is no method matching foo(::Float64), suggesting that the expected argument type is Int64.

julia> TypeError(:sqrt, "calculating square root", Real, "Hello")
ERROR: TypeError: in sqrt, argument 1 is not a real number

In this example, a TypeError is generated with the function name sqrt, context "calculating square root", expected type Real, and actual value "Hello". The error message indicates that the argument passed to the sqrt function is not a real number, as expected.

julia> TypeError(:sin, "computing sine", Number, [1, 2, 3])
ERROR: TypeError: in sin, argument 1 is not a number

This example demonstrates a TypeError with the function name sin, context "computing sine", expected type Number, and actual value [1, 2, 3]. The error message highlights that the argument provided to the sin function is not a number, contrary to the expected type.

It is important to note that the TypeError function is primarily used internally within Julia and is typically not directly called by users. Instead, it is automatically generated when type assertions fail or when intrinsic functions are called with incorrect argument types.

See Also

ArgumentError, AssertionError, BoundsError, DivideError, DomainError, EOFError, error, ErrorException, InexactError, InitError, KeyError, LoadError, MethodError, OutOfMemoryError, OverflowError, ParseError, ReadOnlyMemoryError, showerror, StackOverflowError, SystemError, TypeError, UndefRefError, UndefVarError,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field

Checking you are not a robot: