Why can I call a function with mismatched parameter type?

Q. Schroll qs.il.paperinik at gmail.com
Fri Dec 11 12:24:01 UTC 2020


On Friday, 11 December 2020 at 11:32:09 UTC, rikki cattermole 
wrote:
> string is not a built in type. It is an alias defined by 
> druntime.
>
> https://github.com/dlang/druntime/blob/master/src/object.d#L35
>
> int on the other hand is defined by the compiler. It 
> understands it.

It doesn't magically understand it. `int` is a keyword and thus 
not a legal identifier.
 From a grammar perspective, in `(x, y) { }`, x and y are parsed 
as types. [1] However, in lambda expressions, when there's a type 
only and no parameter (according to the grammar) given, the 
compiler treats a single identifier as a parameter with inferred 
type. Since `int` is not an identifier, but a keyword, that 
treatment does not happen. As you explained correctly, `string` 
is merely an identifier and thus seen as a parameter name.

[1] https://dlang.org/spec/grammar.html#Parameters


More information about the Digitalmars-d-learn mailing list