www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why can't the compiler properly detect the type of

reply Adnan <relay.public.adnan outlook.com> writes:
In the following code the compiler says the type is 
Array!()(string):

			if (key in wordTable)
				wordTable[key] ~= word;
			else
				wordTable[key] = Array!string(word);


source/app.d(29,36): Error: template 
std.container.array.Array!string.Array.__ctor cannot deduce functi
on from argument types !()(string), candidates are:
/snap/dmd/99/bin/../import/phobos/std/container/array.d(467,5):   
      __ctor(U)(U[] values...)
   with U = immutable(char)
   must satisfy the following constraint:
        isImplicitlyConvertible!(U, T)
/snap/dmd/99/bin/../import/phobos/std/container/array.d(501,5):   
      __ctor(Range)(Range r)
   with Range = string
   must satisfy the following constraint:
        isImplicitlyConvertible!(ElementType!Range, T)
Mar 14 2020
next sibling parent Adnan <relay.public.adnan outlook.com> writes:
On Saturday, 14 March 2020 at 23:39:11 UTC, Adnan wrote:
 In the following code the compiler says the type is 
 Array!()(string):

 			if (key in wordTable)
 				wordTable[key] ~= word;
 			else
 				wordTable[key] = Array!string(word);


 source/app.d(29,36): Error: template 
 std.container.array.Array!string.Array.__ctor cannot deduce 
 functi
 on from argument types !()(string), candidates are:
 /snap/dmd/99/bin/../import/phobos/std/container/array.d(467,5):
      __ctor(U)(U[] values...)
   with U = immutable(char)
   must satisfy the following constraint:
        isImplicitlyConvertible!(U, T)
 /snap/dmd/99/bin/../import/phobos/std/container/array.d(501,5):
      __ctor(Range)(Range r)
   with Range = string
   must satisfy the following constraint:
        isImplicitlyConvertible!(ElementType!Range, T)
Full code string smallestRepr(const string arg) { auto repeated = arg ~ arg; string result = arg; foreach (i; 1 .. arg.length) { const slice = repeated[i .. i + arg.length]; if (slice < result) result = slice; } return result; } unittest { assert("cba".smallestRepr() == "acb"); } void main(const string[] args) { import std.stdio : write, File; import std.container : Array; Array!string[string] wordTable; foreach (string word; File(args[1]).byLineCopy()) { word = word[0 .. $ - 1]; // strip the newline character const string key = word.smallestRepr(); debug { if (key in wordTable) wordTable[key] ~= word; else wordTable[key] = Array!string(word); } else { wordTable.require(key, Array!string()) ~= word; } } foreach (array; wordTable.values) { if (array.length == 4) { foreach (word; array) { write(word, ", "); } write("\n"); break; } } }
Mar 14 2020
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 14 March 2020 at 23:39:11 UTC, Adnan wrote:
 Full code
this worked for me when i copy/pasted it... are you sure that has the error? if so what compiler version you on?
Mar 14 2020
parent reply Adnan <relay.public.adnan outlook.com> writes:
On Saturday, 14 March 2020 at 23:54:44 UTC, Adam D. Ruppe wrote:
 On Saturday, 14 March 2020 at 23:39:11 UTC, Adnan wrote:
 Full code
this worked for me when i copy/pasted it... are you sure that has the error? if so what compiler version you on?
This is indeed very strange, godbolt says it's alright: https://d.godbolt.org/z/DaaPxX Now my compiler version: $ dmd --version && ldc2 --version DMD64 D Compiler v2.090.0 Copyright (C) 1999-2019 by The D Language Foundation, All Rights Reserved written by Walter Bright LDC - the LLVM D compiler (1.18.0): based on DMD v2.088.1 and LLVM 9.0.0 built with LDC - the LLVM D compiler (1.18.0) Default target: x86_64-unknown-linux-gnu Host CPU: znver1 http://dlang.org - http://wiki.dlang.org/LDC Registered Targets: aarch64 - AArch64 (little endian) aarch64_32 - AArch64 (little endian ILP32) aarch64_be - AArch64 (big endian) arm - ARM arm64 - ARM64 (little endian) arm64_32 - ARM64 (little endian ILP32) armeb - ARM (big endian) mips - MIPS (32-bit big endian) mips64 - MIPS (64-bit big endian) mips64el - MIPS (64-bit little endian) mipsel - MIPS (32-bit little endian) msp430 - MSP430 [experimental] nvptx - NVIDIA PTX 32-bit nvptx64 - NVIDIA PTX 64-bit ppc32 - PowerPC 32 ppc64 - PowerPC 64 ppc64le - PowerPC 64 LE riscv32 - 32-bit RISC-V riscv64 - 64-bit RISC-V thumb - Thumb thumbeb - Thumb (big endian) wasm32 - WebAssembly 32-bit wasm64 - WebAssembly 64-bit x86 - 32-bit X86: Pentium-Pro and above x86-64 - 64-bit X86: EM64T and AMD64 Note that both ldc2 and dmd has the same complain
Mar 14 2020
parent Adnan <relay.public.adnan outlook.com> writes:
On Sunday, 15 March 2020 at 00:04:09 UTC, Adnan wrote:
 On Saturday, 14 March 2020 at 23:54:44 UTC, Adam D. Ruppe wrote:
 On Saturday, 14 March 2020 at 23:39:11 UTC, Adnan wrote:
 Full code
this worked for me when i copy/pasted it... are you sure that has the error? if so what compiler version you on?
This is indeed very strange, godbolt says it's alright: https://d.godbolt.org/z/DaaPxX Now my compiler version: $ dmd --version && ldc2 --version DMD64 D Compiler v2.090.0 Copyright (C) 1999-2019 by The D Language Foundation, All Rights Reserved written by Walter Bright LDC - the LLVM D compiler (1.18.0): based on DMD v2.088.1 and LLVM 9.0.0 built with LDC - the LLVM D compiler (1.18.0) Default target: x86_64-unknown-linux-gnu Host CPU: znver1 http://dlang.org - http://wiki.dlang.org/LDC Registered Targets: aarch64 - AArch64 (little endian) aarch64_32 - AArch64 (little endian ILP32) aarch64_be - AArch64 (big endian) arm - ARM arm64 - ARM64 (little endian) arm64_32 - ARM64 (little endian ILP32) armeb - ARM (big endian) mips - MIPS (32-bit big endian) mips64 - MIPS (64-bit big endian) mips64el - MIPS (64-bit little endian) mipsel - MIPS (32-bit little endian) msp430 - MSP430 [experimental] nvptx - NVIDIA PTX 32-bit nvptx64 - NVIDIA PTX 64-bit ppc32 - PowerPC 32 ppc64 - PowerPC 64 ppc64le - PowerPC 64 LE riscv32 - 32-bit RISC-V riscv64 - 64-bit RISC-V thumb - Thumb thumbeb - Thumb (big endian) wasm32 - WebAssembly 32-bit wasm64 - WebAssembly 64-bit x86 - 32-bit X86: Pentium-Pro and above x86-64 - 64-bit X86: EM64T and AMD64 Note that both ldc2 and dmd has the same complain
d$ dmd --version && cat source/app.d && dub build DMD64 D Compiler v2.090.0 Copyright (C) 1999-2019 by The D Language Foundation, All Rights Reserved written by Walter Bright string smallestRepr(const string arg) { auto repeated = arg ~ arg; string result = arg; foreach (i; 1 .. arg.length) { const slice = repeated[i .. i + arg.length]; if (slice < result) result = slice; } return result; } unittest { assert("cba".smallestRepr() == "acb"); } void main(const string[] args) { import std.stdio : write, File; import std.container : Array; Array!string[string] wordTable; foreach (string word; File(args[1]).byLineCopy()) { word = word[0 .. $ - 1]; // strip the newline character const string key = word.smallestRepr(); debug { if (key in wordTable) wordTable[key] ~= word; else wordTable[key] = Array!string(word); } else { wordTable.require(key, Array!string()) ~= word; } } foreach (array; wordTable.values) { if (array.length == 4) { foreach (word; array) { write(word, ", "); } write("\n"); break; } } } Performing "debug" build using dmd for x86_64. dbed ~master: building configuration "application"... source/app.d(29,34): Error: template std.container.array.Array!string.Array.__ctor cannot deduce function from argument types !()(string), candidates are: /snap/dmd/99/bin/../import/phobos/std/container/array.d(467,5): __ctor(U)(U[] values...) with U = immutable(char) must satisfy the following constraint: isImplicitlyConvertible!(U, T) /snap/dmd/99/bin/../import/phobos/std/container/array.d(501,5): __ctor(Range)(Range r) with Range = string must satisfy the following constraint: isImplicitlyConvertible!(ElementType!Range, T) source/app.d(36,2): Warning: statement is not reachable dmd failed with exit code 1.
Mar 14 2020