digitalmars.D.learn - "function ... is not callable using argument types"
- Suliman (47/47) Jan 01 2015 I need to pass some config to ddbc driver. When I use static
- Tobias Pankrath (4/10) Jan 01 2015 int does not implicitly convert to short. It does in the
- Suliman (4/4) Jan 01 2015 But why variant:
- Tobias Pankrath (7/11) Jan 01 2015 As I said the compiler infers that 5432 is between short.min and
- bearophile (6/10) Jan 01 2015 Because value range analysis now propagates the range even across
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (4/7) Jan 01 2015 Walter Bright explains the reasons in his "Value Range Propagation" arti...
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...
Jan 01 2015
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.
Jan 01 2015
But why variant: static const int PGSQL_UNITTEST_PORT = 5432; do not require of implicit convert to!short() at connection string?
Jan 01 2015
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;
Jan 01 2015
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
Jan 01 2015
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
Jan 01 2015