Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
January 01, 2015 "function ... is not callable using argument types" | ||||
---|---|---|---|---|
| ||||
I need to pass some config to ddbc driver. When I use static const all work ok. static const string PGSQL_UNITTEST_HOST = "localhost"; static const int PGSQL_UNITTEST_PORT = 5432; static const string PGSQL_UNITTEST_USER = "postgres"; static const string PGSQL_UNITTEST_PASSWORD = "Infinity8"; static const string PGSQL_UNITTEST_DB = "test2"; But using static const mean that settings will be hardcoded. But I need to read it from config file. So I need so simply declared it as: this(parseConfig parseconfig) { string PGSQL_UNITTEST_HOST = parseconfig.dbhost; int PGSQL_UNITTEST_PORT = parseconfig.dbport; string PGSQL_UNITTEST_USER = parseconfig.dbuser; string PGSQL_UNITTEST_PASSWORD = parseconfig.dbpass; string PGSQL_UNITTEST_DB = parseconfig.dbname; ... } But when I do it like above code stop compile and I am getting error: source\app.d(218): Error: function ddbc.drivers.pgsqlddbc.PGSQLDriver.generateUrl (string host, ushort port, string dbname) is not callable using argument types (string, int, string) I looked at driver source code and have found next code: class PGSQLDriver : Driver { // helper function public static string generateUrl(string host, ushort port, string dbname) { return "postgresql://" ~ host ~ ":" ~ to!string(port) ~ "/" ~ dbname; } public static string[string] setUserAndPassword(string username, string password) { string[string] params; params["user"] = username; params["password"] = password; params["ssl"] = "true"; return params; } So it's look like that it can accept strings and ints without problem. And I really can't understand why it's accept only "static const string" constructions... |
January 01, 2015 Re: "function ... is not callable using argument types" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman |
>
> So it's look like that it can accept strings and ints without problem.
>
> And I really can't understand why it's accept only "static const string" constructions...
>
int does not implicitly convert to short. It does in the hardcoded version, because the compiler can prove that the value is between short.min and short.max.
Use to!short() to convert it to short.
|
January 01, 2015 Re: "function ... is not callable using argument types" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tobias Pankrath | But why variant: static const int PGSQL_UNITTEST_PORT = 5432; do not require of implicit convert to!short() at connection string? |
January 01, 2015 Re: "function ... is not callable using argument types" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Thursday, 1 January 2015 at 13:09:21 UTC, Suliman wrote:
> But why variant:
> static const int PGSQL_UNITTEST_PORT = 5432;
>
> do not require of implicit convert to!short() at connection string?
As I said the compiler infers that 5432 is between short.min and short.max. Try it with something out of this range.
BTW: If you just want to have a global constant, I'd use enum or immutable:
enum PGSQL_UNITTEST_PORT = 5432;
immutable PGSQL_UNITTEST_PORT = 5432;
|
January 01, 2015 Re: "function ... is not callable using argument types" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | Suliman:
> But why variant:
> static const int PGSQL_UNITTEST_PORT = 5432;
>
> do not require of implicit convert to!short() at connection string?
Because value range analysis now propagates the range even across expressions if they are const. It's a recent improvement to make the D compile a bit less stupid.
Bye,
bearophile
|
January 01, 2015 Re: "function ... is not callable using argument types" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | On 01/01/2015 05:09 AM, Suliman wrote: > But why variant: > static const int PGSQL_UNITTEST_PORT = 5432; > > do not require of implicit convert to!short() at connection string? Walter Bright explains the reasons in his "Value Range Propagation" article: http://www.drdobbs.com/tools/value-range-propagation/229300211 Ali |
Copyright © 1999-2021 by the D Language Foundation