www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Cannot deduce from type

reply "Chris" <wendlec tcd.ie> writes:
Why is that?

import std.stdio, std.array

void main() {
   auto output = appender!(string);
   output ~= "world!";
//  output.data.insertInPlace(0, "Hello, ");  // Doesn't work
   auto asString = output.data;
   asString.insertInPlace(0, "Hello, ");  // Works
   writeln(asString);  // prints "Hello, world!"
}

Error: template std.array.insertInPlace cannot deduce function 
from argument types !()(string, int, string), candidates are:
.dvm/compilers/dmd-2.066.0/linux/bin/../../src/phobos/std/array.d(1031): 
        std.array.insertInPlace(T, U...)(ref T[] array, size_t 
pos, U stuff) if (!isSomeString!(T[]) && 
allSatisfy!(isInputRangeOrConvertible!T, U) && U.length > 0)
.dvm/compilers/dmd-2.066.0/linux/bin/../../src/phobos/std/array.d(1097): 
        std.array.insertInPlace(T, U...)(ref T[] array, size_t 
pos, U stuff) if (isSomeString!(T[]) && 
allSatisfy!(isCharOrStringOrDcharRange, U))
Sep 22 2014
next sibling parent reply "anonymous" <anonymous example.com> writes:
On Monday, 22 September 2014 at 14:45:31 UTC, Chris wrote:
 Why is that?

 import std.stdio, std.array

 void main() {
   auto output = appender!(string);
   output ~= "world!";
 //  output.data.insertInPlace(0, "Hello, ");  // Doesn't work
   auto asString = output.data;
   asString.insertInPlace(0, "Hello, ");  // Works
   writeln(asString);  // prints "Hello, world!"
 }

 Error: template std.array.insertInPlace cannot deduce function 
 from argument types !()(string, int, string), candidates are:
 .dvm/compilers/dmd-2.066.0/linux/bin/../../src/phobos/std/array.d(1031):
        std.array.insertInPlace(T, U...)(ref T[] array, size_t 
 pos, U stuff) if (!isSomeString!(T[]) && 
 allSatisfy!(isInputRangeOrConvertible!T, U) && U.length > 0)
 .dvm/compilers/dmd-2.066.0/linux/bin/../../src/phobos/std/array.d(1097):
        std.array.insertInPlace(T, U...)(ref T[] array, size_t 
 pos, U stuff) if (isSomeString!(T[]) && 
 allSatisfy!(isCharOrStringOrDcharRange, U))
output.data is a method returning a string. That is, the returned string is not an lvalue, it doesn't have an address, it cannot be passed in a ref parameter. But insertInPlace's first parameter is ref. So, no can do. Also note that `writeln(output.data);` prints "world!" without "Hello, ".
Sep 22 2014
parent "Chris" <wendlec tcd.ie> writes:
On Monday, 22 September 2014 at 15:00:09 UTC, anonymous wrote:
 On Monday, 22 September 2014 at 14:45:31 UTC, Chris wrote:
 Why is that?

 import std.stdio, std.array

 void main() {
  auto output = appender!(string);
  output ~= "world!";
 //  output.data.insertInPlace(0, "Hello, ");  // Doesn't work
  auto asString = output.data;
  asString.insertInPlace(0, "Hello, ");  // Works
  writeln(asString);  // prints "Hello, world!"
 }

 Error: template std.array.insertInPlace cannot deduce function 
 from argument types !()(string, int, string), candidates are:
 .dvm/compilers/dmd-2.066.0/linux/bin/../../src/phobos/std/array.d(1031):
       std.array.insertInPlace(T, U...)(ref T[] array, size_t 
 pos, U stuff) if (!isSomeString!(T[]) && 
 allSatisfy!(isInputRangeOrConvertible!T, U) && U.length > 0)
 .dvm/compilers/dmd-2.066.0/linux/bin/../../src/phobos/std/array.d(1097):
       std.array.insertInPlace(T, U...)(ref T[] array, size_t 
 pos, U stuff) if (isSomeString!(T[]) && 
 allSatisfy!(isCharOrStringOrDcharRange, U))
output.data is a method returning a string. That is, the returned string is not an lvalue, it doesn't have an address, it cannot be passed in a ref parameter. But insertInPlace's first parameter is ref. So, no can do.
I see. Thanks.
 Also note that `writeln(output.data);` prints "world!" without
 "Hello, ".
Because I only append "world!" to it. You would have to print the variable "asString" to get "Hello, world!"
Sep 22 2014
prev sibling next sibling parent "kiran kumari" <kiranfabzen gmail.com> writes:
On Monday, 22 September 2014 at 14:45:31 UTC, Chris wrote:
 Why is that?

 import std.stdio, std.array

 void main() {
   auto output = appender!(string);
   output ~= "world!";
 //  output.data.insertInPlace(0, "Hello, ");  // Doesn't work
   auto asString = output.data;
   asString.insertInPlace(0, "Hello, ");  // Works
   writeln(asString);  // prints "Hello, world!"
 }

 Error: template std.array.insertInPlace cannot deduce function 
 from argument types !()(string, int, string), candidates are:
 .dvm/compilers/dmd-2.066.0/linux/bin/../../src/phobos/std/array.d(1031):
        std.array.insertInPlace(T, U...)(ref T[] array, size_t 
 pos, U stuff) if (!isSomeString!(T[]) && 
 allSatisfy!(isInputRangeOrConvertible!T, U) && U.length > 0)
 .dvm/compilers/dmd-2.066.0/linux/bin/../../src/phobos/std/array.d(1097):
        std.array.insertInPlace(T, U...)(ref T[] array, size_t 
 pos, U stuff) if (isSomeString!(T[]) && 
 allSatisfy!(isCharOrStringOrDcharRange, U))
see ore example ://techgurulab.com/course/java-quiz-online/ore example
Sep 24 2014
prev sibling parent "kiran kumari" <kiranfabzen gmail.com> writes:
On Monday, 22 September 2014 at 14:45:31 UTC, Chris wrote:
 Why is that?

 import std.stdio, std.array

 void main() {
   auto output = appender!(string);
   output ~= "world!";
 //  output.data.insertInPlace(0, "Hello, ");  // Doesn't work
   auto asString = output.data;
   asString.insertInPlace(0, "Hello, ");  // Works
   writeln(asString);  // prints "Hello, world!"
 }

 Error: template std.array.insertInPlace cannot deduce function 
 from argument types !()(string, int, string), candidates are:
 .dvm/compilers/dmd-2.066.0/linux/bin/../../src/phobos/std/array.d(1031):
        std.array.insertInPlace(T, U...)(ref T[] array, size_t 
 pos, U stuff) if (!isSomeString!(T[]) && 
 allSatisfy!(isInputRangeOrConvertible!T, U) && U.length > 0)
 .dvm/compilers/dmd-2.066.0/linux/bin/../../src/phobos/std/array.d(1097):
        std.array.insertInPlace(T, U...)(ref T[] array, size_t 
 pos, U stuff) if (isSomeString!(T[]) && 
 allSatisfy!(isCharOrStringOrDcharRange, U))
see more example http://techgurulab.com/course/java-quiz-online/
Sep 24 2014