diff --git a/NEWS.md b/NEWS.md index 25c513d1a5a30..9205c406092b1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,8 @@ Julia v1.6 Release Notes New language features --------------------- +* Types written with `where` syntax can now be used to define constructors, e.g. + `(Foo{T} where T)(x) = ...`. Language changes ---------------- diff --git a/src/julia-syntax.scm b/src/julia-syntax.scm index ec1c5af9eca3d..22317ef989068 100644 --- a/src/julia-syntax.scm +++ b/src/julia-syntax.scm @@ -1111,7 +1111,7 @@ (and (not (any kwarg? argl)) (not (and (pair? argl) (pair? (car argl)) (eq? (caar argl) 'parameters)))))) - (name (if (or (decl? name) (and (pair? name) (eq? (car name) 'curly))) + (name (if (or (decl? name) (and (pair? name) (memq (car name) '(curly where)))) #f name))) (expand-forms (method-def-expr name sparams argl body rett)))) diff --git a/test/core.jl b/test/core.jl index 98344bfb44506..f183db7ac31f4 100644 --- a/test/core.jl +++ b/test/core.jl @@ -4708,6 +4708,11 @@ let ft = Base.datatype_fieldtypes @test !isdefined(ft(B12238.body.body)[1], :instance) # has free type vars end +# `where` syntax in constructor definitions +(A12238{T} where T<:Real)(x) = 0 +@test A12238{<:Real}(0) == 0 +@test_throws MethodError A12238{<:Integer}(0) + # issue #16315 let a = Any[] @noinline f() = a[end]