Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Add ArrayLike #34196

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
## Basic functions ##

"""
AbstractArray{T,N}
ArrayLike{N}

Supertype for `N`-dimensional arrays (or array-like types) with or without a
pre-defined element type. For arrays with a pre-defined eltype, use
[`AbstractArray`](@ref).
"""
ArrayLike

"""
AbstractArray{T,N} <: ArrayLike{N}

Supertype for `N`-dimensional arrays (or array-like types) with elements of type `T`.
[`Array`](@ref) and other types are subtypes of this. See the manual section on the
[`AbstractArray` interface](@ref man-interface-array).
[`AbstractArray` interface](@ref man-interface-array). For arrays without a
pre-defined eltype, use [`ArrayLike`](@ref).
"""
AbstractArray

Expand Down
5 changes: 3 additions & 2 deletions base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
#end
#const nothing = Nothing()

#abstract type AbstractArray{T,N} end
#abstract type ArrayLike{N} end
#abstract type AbstractArray{T,N} <: ArrayLike{N} end
#abstract type DenseArray{T,N} <: AbstractArray{T,N} end

#mutable struct Array{T,N} <: DenseArray{T,N}
Expand Down Expand Up @@ -146,7 +147,7 @@ export
# key types
Any, DataType, Vararg, NTuple,
Tuple, Type, UnionAll, TypeVar, Union, Nothing, Cvoid,
AbstractArray, DenseArray, NamedTuple,
ArrayLike, AbstractArray, DenseArray, NamedTuple,
# special objects
Function, Method,
Module, Symbol, Task, Array, UndefInitializer, undef, WeakRef, VecElement,
Expand Down
1 change: 1 addition & 0 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,7 @@ void jl_init_primitives(void) JL_GC_DISABLED
add_builtin("Ptr", (jl_value_t*)jl_pointer_type);
add_builtin("Task", (jl_value_t*)jl_task_type);

add_builtin("ArrayLike", (jl_value_t*)jl_arraylike_type);
add_builtin("AbstractArray", (jl_value_t*)jl_abstractarray_type);
add_builtin("DenseArray", (jl_value_t*)jl_densearray_type);
add_builtin("Array", (jl_value_t*)jl_array_type);
Expand Down
3 changes: 2 additions & 1 deletion src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -3344,7 +3344,7 @@ void jl_init_serializer(void)

jl_bool_type, jl_linenumbernode_type, jl_pinode_type,
jl_upsilonnode_type, jl_type_type, jl_bottom_type, jl_ref_type,
jl_pointer_type, jl_vararg_type, jl_abstractarray_type, jl_void_type,
jl_pointer_type, jl_vararg_type, jl_arraylike_type, jl_abstractarray_type, jl_void_type,
jl_densearray_type, jl_function_type, jl_typename_type,
jl_builtin_type, jl_task_type, jl_uniontype_type, jl_typetype_type,
jl_array_any_type, jl_intrinsic_type,
Expand Down Expand Up @@ -3416,6 +3416,7 @@ void jl_init_serializer(void)
arraylist_push(&builtin_typenames, ((jl_datatype_t*)jl_ref_type->body)->name);
arraylist_push(&builtin_typenames, jl_pointer_typename);
arraylist_push(&builtin_typenames, jl_type_typename);
arraylist_push(&builtin_typenames, ((jl_datatype_t*)jl_unwrap_unionall((jl_value_t*)jl_arraylike_type))->name);
arraylist_push(&builtin_typenames, ((jl_datatype_t*)jl_unwrap_unionall((jl_value_t*)jl_abstractarray_type))->name);
arraylist_push(&builtin_typenames, ((jl_datatype_t*)jl_unwrap_unionall((jl_value_t*)jl_densearray_type))->name);
arraylist_push(&builtin_typenames, jl_tuple_typename);
Expand Down
13 changes: 12 additions & 1 deletion src/jltypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ jl_datatype_t *jl_builtin_type;

jl_datatype_t *jl_typeofbottom_type;
jl_value_t *jl_bottom_type;
jl_unionall_t *jl_arraylike_type;
jl_unionall_t *jl_abstractarray_type;
jl_unionall_t *jl_densearray_type;

Expand Down Expand Up @@ -1983,10 +1984,20 @@ void jl_init_types(void) JL_GC_DISABLED
jl_function_type->name->mt = NULL; // subtypes of Function have independent method tables
jl_builtin_type->name->mt = NULL; // so they don't share the Any type table

tv = jl_svec1(tvar("N"));
jl_arraylike_type = (jl_unionall_t*)
jl_new_abstracttype((jl_value_t*)jl_symbol("ArrayLike"), core,
jl_any_type, tv)->name->wrapper;

jl_tvar_t *tv_T = tvar("T");
jl_tvar_t *tv_N = tvar("N");
tv = jl_svec2(tv_T, tv_N);

tv = jl_svec2(tvar("T"), tvar("N"));
jl_abstractarray_type = (jl_unionall_t*)
jl_new_abstracttype((jl_value_t*)jl_symbol("AbstractArray"), core,
jl_any_type, tv)->name->wrapper;
(jl_datatype_t*)jl_apply_type((jl_value_t*)jl_arraylike_type, jl_svec_data(jl_svec1(tv_N)), 1),
tv)->name->wrapper;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote this code by guess and check because I don't know C, but it seemed to work

tv = jl_svec2(tvar("T"), tvar("N"));
jl_densearray_type = (jl_unionall_t*)
Expand Down
1 change: 1 addition & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ extern JL_DLLEXPORT jl_datatype_t *jl_code_instance_type JL_GLOBALLY_ROOTED;
extern JL_DLLEXPORT jl_datatype_t *jl_code_info_type JL_GLOBALLY_ROOTED;
extern JL_DLLEXPORT jl_datatype_t *jl_method_type JL_GLOBALLY_ROOTED;
extern JL_DLLEXPORT jl_datatype_t *jl_module_type JL_GLOBALLY_ROOTED;
extern JL_DLLEXPORT jl_unionall_t *jl_arraylike_type JL_GLOBALLY_ROOTED;
extern JL_DLLEXPORT jl_unionall_t *jl_abstractarray_type JL_GLOBALLY_ROOTED;
extern JL_DLLEXPORT jl_unionall_t *jl_densearray_type JL_GLOBALLY_ROOTED;
extern JL_DLLEXPORT jl_unionall_t *jl_array_type JL_GLOBALLY_ROOTED;
Expand Down
2 changes: 1 addition & 1 deletion src/staticdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static void *const _tags[] = {
&jl_gotonode_type, &jl_quotenode_type,
&jl_pinode_type, &jl_phinode_type, &jl_phicnode_type, &jl_upsilonnode_type,
&jl_type_type, &jl_bottom_type, &jl_ref_type, &jl_pointer_type,
&jl_vararg_type, &jl_abstractarray_type,
&jl_vararg_type, &jl_arraylike_type, &jl_abstractarray_type,
&jl_densearray_type, &jl_void_type, &jl_function_type, &jl_typeofbottom_type,
&jl_unionall_type, &jl_typename_type, &jl_builtin_type, &jl_code_info_type,
&jl_task_type, &jl_uniontype_type, &jl_typetype_type, &jl_abstractstring_type,
Expand Down
2 changes: 1 addition & 1 deletion test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1348,7 +1348,7 @@ end
@test sprint(show, Main) == "Main"

@test sprint(Base.show_supertypes, Int64) == "Int64 <: Signed <: Integer <: Real <: Number <: Any"
@test sprint(Base.show_supertypes, Vector{String}) == "Array{String,1} <: DenseArray{String,1} <: AbstractArray{String,1} <: Any"
@test sprint(Base.show_supertypes, Vector{String}) == "Array{String,1} <: DenseArray{String,1} <: AbstractArray{String,1} <: ArrayLike{1} <: Any"

# static_show

Expand Down