[英]ERROR DART: The name 'x' isn't a type so it can't be used as a type argument
Error in the Input part in one class and in Output in another class一个 class 和另一个 class 中的 Output 中的输入部分出错
abstract class HttpFunction<Input, Output>
{
Output apply(Input paramInput);
}
class ComposerFunction with HttpFunction<Input, Request>
{
RequestComposer<Input> inputConverter;
ComposerFunction(RequestComposer inputConverter);
ComposerFunction_(RequestComposer<Input> inputConverter)
{
this.inputConverter = inputConverter;
}
Request apply(Input input)
{
return this.inputConverter.compose(input);
}
}
class InterpreterFunction with HttpFunction<Response, Output>
{
ResponseInterpreter<Output> responseInterpreter;
InterpreterFunction(ResponseInterpreter outputConverter);
InterpreterFunction_(ResponseInterpreter<Output> responseInterpreter)
{
this.responseInterpreter = responseInterpreter;
}
Output apply(Response response)
{
return this.responseInterpreter.interpret(response);
}
}
The name 'Input' isn't a type so it can't be used as a type argument.
Try correcting the name to an existing type, or defining a type named 'Input'.dart(non_type_as_type_argument)
And和
The name 'Output' isn't a type so it can't be used as a type argument.
Try correcting the name to an existing type, or defining a type named 'Output'.dart(non_type_as_type_argument)
UPDATE更新
class ComposerFunction<Input> implements HttpFunction<Input, Request>
{
...
}
class InterpreterFunction<Output> implements HttpFunction<Response, Output>
{
...
}
Thank you for your answers:)谢谢您的回答:)
If generically passing types is what you're after, could you try changing your abstract class as below如果一般传递类型是您所追求的,您是否可以尝试更改您的抽象 class 如下
abstract class HttpFunction<T, U>
{
U apply<T, U>(T paramInput);
}
And the classes as follows:类如下:
class ComposerFunction with HttpFunction<Input, Request>
{
RequestComposer<Input> inputConverter;
ComposerFunction(RequestComposer inputConverter);
ComposerFunction_(RequestComposer<Input> inputConverter)
{
this.inputConverter = inputConverter;
}
Request apply<Input, Request>(Input input)
{
return this.inputConverter.compose(input);
}
}
class InterpreterFunction with HttpFunction<Response, Output>
{
ResponseInterpreter<Output> responseInterpreter;
InterpreterFunction(ResponseInterpreter outputConverter);
InterpreterFunction_(ResponseInterpreter<Output> responseInterpreter)
{
this.responseInterpreter = responseInterpreter;
}
Output apply<Response, Output>(Response response)
{
return this.responseInterpreter.interpret(response);
}
}
This is assuming you have classes created/referenced of the expected types这是假设您创建/引用了预期类型的类
You have:你有:
class ComposerFunction with HttpFunction<Input, Request>
{
...
}
Assuming that you want ComposerFunction
to be a generic class that can be parameterized on caller-specified Input
and Request
types, you have not declared ComposerFunction
to be a generic class.假设您希望
ComposerFunction
成为通用 class 可以在调用者指定的Input
和Request
类型上进行参数化,您尚未将ComposerFunction
声明为通用 class。 Although HttpFunction
is a generic class, ComposerFunction
as written is a non -generic class that uses HttpFunction
specialized with types literally named Input
and Request
.尽管
HttpFunction
是一个通用的 class,但ComposerFunction
是一个非通用的HttpFunction
,它使用专门的类型为Input
和Request
的 HttpFunction 。 If you don't have classes named Input
and Request
, this therefore fails with:如果您没有名为
Input
和Request
的类,那么这将失败并显示:
The name 'Input' isn't a type so it can't be used as a type argument.
名称“输入”不是类型,因此不能用作类型参数。
to make ComposerFunction
a generic class, you need to add type parameters to it :要使
ComposerFunction
成为通用 class,您需要向其添加类型参数:
class ComposerFunction<Input, Request> with HttpFunction<Input, Request>
{
...
}
Same thing applies to InterpreterFunction
.同样的事情也适用于
InterpreterFunction
。
Additionally, with
is normally intended for use with mixin
s .此外,
with
通常用于mixin
。 It's also allowed with abstract class
for backward compatibility with Dart 2.0 and earlier, but since HttpFunction
does not provide any code that can be reused, this doesn't seem useful. abstract class
也允许与 Dart 2.0 及更早版本向后兼容,但由于HttpFunction
不提供任何可重用的代码,这似乎没有用。 If you want ComposerFunction
(and InterpreterFunction
) to conform to an interface, then you usually would use implements
instead.如果您希望
ComposerFunction
(和InterpreterFunction
)符合接口,那么您通常会改用implements
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.