parsing - How does Swift disambiguate generic constructors? -
consider following swift expression
println(generic<foo, bar>(1))
normally, 1 read generic call constructor generic<foo, bar>
arguments (1)
.
println( generic<foo,bar>(1) )
however, when re-arranging tokens bit, represent 2 separate comparisons, example if generic
, foo
poorly named numeric variables:
println(generic < foo, bar > (1)) // or, proper parenthesis println((generic < foo), (bar > 1))
what can observe here expression generic constructor highly ambiguous, , not easy disambiguate humans. problem here swift doesn't have new
keyword constructors, makes them ambiguous method calls , operators in cases. thus, interested in how swift compiler (parser) manages disambiguate above expressions. way parsed dependant on context (types, variables, functions) or can resolved parser?
the answer simple: compiler does't allow declare these variables:
struct generic<t, u> { init(_ i: int) {} } struct foo {} struct bar {} print(generic<foo, bar>(1)) // error let foo = 0 // invalid redeclaration of foo let bar = 3 // invalid redeclaration of bar let generic = 5 // invalid redeclaration of generic print(generic<foo, bar>(1))
making either variables or type declarations in source file current declaration "overrides" other one.
Comments
Post a Comment