Skip to content

Commit f28ddaa

Browse files
committed
Merge pull request #5164 from porterjamesj/tuptypes
RFC: Allow tuples as type parameters.
2 parents 2b2b42c + 7637526 commit f28ddaa

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

doc/manual/types.rst

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. _man-types:
22

33
*********
4-
Types
4+
Types
55
*********
66

77
Type systems have traditionally fallen into two quite different camps:
@@ -61,7 +61,7 @@ Julia's type system that should be mentioned up front are:
6161
- Only values, not variables, have types — variables are simply names
6262
bound to values.
6363
- Both abstract and concrete types can be paramaterized by other types
64-
and by certain other values (currently integers and symbols).
64+
and by certain other values (currently integers, symbols, bools, and tuples thereof).
6565
Type parameters may be completely omitted when they
6666
do not need to be referenced or restricted.
6767

@@ -1153,4 +1153,3 @@ If you apply ``super`` to other type objects (or non-type objects), a
11531153

11541154
julia> super((Float64,Int64))
11551155
ERROR: no method super(Type{(Float64,Int64)})
1156-

src/jltypes.c

+12-2
Original file line numberDiff line numberDiff line change
@@ -1447,8 +1447,18 @@ int jl_types_equal_generic(jl_value_t *a, jl_value_t *b, int useenv)
14471447

14481448
static int valid_type_param(jl_value_t *v)
14491449
{
1450-
// TODO: maybe more things
1451-
return jl_is_type(v) || jl_is_long(v) || jl_is_symbol(v) || jl_is_typevar(v) || jl_is_bool(v);
1450+
if (jl_is_tuple(v)) {
1451+
size_t i;
1452+
size_t l = jl_tuple_len(v);
1453+
for(i=0; i < l; i++) {
1454+
if (!valid_type_param(jl_tupleref(v,i)))
1455+
return 0;
1456+
}
1457+
return 1;
1458+
} else {
1459+
// TODO: maybe more things
1460+
return jl_is_type(v) || jl_is_long(v) || jl_is_symbol(v) || jl_is_typevar(v) || jl_is_bool(v);
1461+
}
14521462
}
14531463

14541464
jl_value_t *jl_apply_type_(jl_value_t *tc, jl_value_t **params, size_t n)

test/core.jl

+24
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,7 @@ end
11811181
f5150(T) = Array(Rational{T},1)
11821182
@test typeof(f5150(Int)) === Array{Rational{Int},1}
11831183

1184+
11841185
# issue #5165
11851186
bitstype 64 T5165{S}
11861187
make_t(x::Int64) = Base.box(T5165{Nothing}, Base.unbox(Int64, x))
@@ -1189,3 +1190,26 @@ b5165 = IOBuffer()
11891190
for x in xs5165
11901191
println(b5165, x) # segfaulted
11911192
end
1193+
1194+
# support tuples as type parameters
1195+
1196+
type TupleParam{P}
1197+
x::Bool
1198+
end
1199+
1200+
function tupledispatch(a::TupleParam{(1,:a)})
1201+
a.x
1202+
end
1203+
1204+
let
1205+
# tuples can be used as type params
1206+
t1 = TupleParam{(1,:a)}(true)
1207+
t2 = TupleParam{(1,:b)}(true)
1208+
1209+
# tuple type params can't contain invalid type params
1210+
@test_throws t3 = TupleParam{(1,"nope")}(true)
1211+
1212+
# dispatch works properly
1213+
@test tupledispatch(t1) == true
1214+
@test_throws tupledispatch(t2)
1215+
end

0 commit comments

Comments
 (0)