return-type

How to return a class instance in member function of a class?

℡╲_俬逩灬. 提交于 2020-07-11 05:14:36
问题 I want to return a class instance in member function of a class, my code is: class MyClass(object): def __init__(self, *args, **kwargs): [snippet] def func(self, *args, **kwargs): [snippet] return class_instnace_of_MyClass if __name__ == '__main__': obj = MyClass(...) newobj = obj.func(...) # type(newobj) is MyClass I think I can call __init__() in func() , and return a new instance of MyClass, but I don't think it is a Pythonic way to do so. How should I do that? Thank you! 回答1: I feel like

How to return a class instance in member function of a class?

假如想象 提交于 2020-07-11 05:13:14
问题 I want to return a class instance in member function of a class, my code is: class MyClass(object): def __init__(self, *args, **kwargs): [snippet] def func(self, *args, **kwargs): [snippet] return class_instnace_of_MyClass if __name__ == '__main__': obj = MyClass(...) newobj = obj.func(...) # type(newobj) is MyClass I think I can call __init__() in func() , and return a new instance of MyClass, but I don't think it is a Pythonic way to do so. How should I do that? Thank you! 回答1: I feel like

Type Constructor as Return Type

 ̄綄美尐妖づ 提交于 2020-07-05 07:22:43
问题 In Scala, I can define an Algebraic Data Type: scala> sealed trait Maybe[A] defined trait Maybe scala> case class Just[A](x: A) extends Maybe[A] defined class Just scala> case object NothingHere extends Maybe[Nothing] defined object NothingHere It's possible to return a function, f , with a return type of Maybe[A] . scala> def f[A](x: A): Maybe[A] = Just(x) f: [A](x: A)Maybe[A] However, it's also possible to specify that a Just[A] is returned. scala> def f[A](x: A): Just[A] = Just(x) f: [A](x

Type Constructor as Return Type

佐手、 提交于 2020-07-05 07:21:29
问题 In Scala, I can define an Algebraic Data Type: scala> sealed trait Maybe[A] defined trait Maybe scala> case class Just[A](x: A) extends Maybe[A] defined class Just scala> case object NothingHere extends Maybe[Nothing] defined object NothingHere It's possible to return a function, f , with a return type of Maybe[A] . scala> def f[A](x: A): Maybe[A] = Just(x) f: [A](x: A)Maybe[A] However, it's also possible to specify that a Just[A] is returned. scala> def f[A](x: A): Just[A] = Just(x) f: [A](x

Why can't 'kotlin.Result' be used as a return type?

回眸只為那壹抹淺笑 提交于 2020-04-28 02:13:47
问题 I've created a method, and the return is Result<R> in a class of MyClass<R> , but the error message is 'kotlin.Result' cannot be used as a return type I've also looked into the Result source code for some hints; why is this so? Test code (using v. 1.3-RC). class MyClass<R>(val r:R){ fun f():Result<R>{ // error here return Result.success(r) } } fun main(args: Array<String>) { val s = Result.success(1) val m = MyClass(s) } 回答1: From the Kotlin KEEP: The rationale behind these limitations is

Why can't 'kotlin.Result' be used as a return type?

China☆狼群 提交于 2020-04-28 02:12:55
问题 I've created a method, and the return is Result<R> in a class of MyClass<R> , but the error message is 'kotlin.Result' cannot be used as a return type I've also looked into the Result source code for some hints; why is this so? Test code (using v. 1.3-RC). class MyClass<R>(val r:R){ fun f():Result<R>{ // error here return Result.success(r) } } fun main(args: Array<String>) { val s = Result.success(1) val m = MyClass(s) } 回答1: From the Kotlin KEEP: The rationale behind these limitations is

Incomplete types as function parameters and return values

99封情书 提交于 2020-04-09 15:50:36
问题 The following code compiles successfully both with clang++ 5.0.0 and g++ 7.2 (with the -std=c++17 -Wall -Wextra -Werror -pedantic-errors -O0 compilation flags): struct Foo; struct Bar { Foo get() const; void set(Foo); }; struct Foo { }; Foo Bar::get() const { return {}; } void Bar::set(Foo) { } int main() { Bar bar{}; (void)bar.get(); bar.set(Foo{}); } Is it valid to use incomplete types as function parameters and return values? What does the C++ say on it? 回答1: In a function definition , you

Incomplete types as function parameters and return values

北战南征 提交于 2020-04-09 15:49:12
问题 The following code compiles successfully both with clang++ 5.0.0 and g++ 7.2 (with the -std=c++17 -Wall -Wextra -Werror -pedantic-errors -O0 compilation flags): struct Foo; struct Bar { Foo get() const; void set(Foo); }; struct Foo { }; Foo Bar::get() const { return {}; } void Bar::set(Foo) { } int main() { Bar bar{}; (void)bar.get(); bar.set(Foo{}); } Is it valid to use incomplete types as function parameters and return values? What does the C++ say on it? 回答1: In a function definition , you

Method return type contains subclass definition

会有一股神秘感。 提交于 2020-01-25 03:41:11
问题 I come up with a code that uses a syntax like this: public <A extends B> double[][] foo(C arg) { .... } I get a couple of questions by viewing it. a) The return type of foo(C arg) is <A extends B> double[][] ? What does this mean? I would understood a return type like double[][] for example, but I cannot determine what does the previous modifier (maybe?) <A extends B> does? b) Why there is a subclass declaration inside a return type? Since A is a subclass of B where do we override or add any

Dynamic Return Type in Java method

你。 提交于 2020-01-22 07:21:29
问题 I've seen a question similar to this multiple times here, but there is one big difference. In the other questions, the return type is to be determined by the parameter. What I want/need to do is determine the return type by the parsed value of a byte[] . From what I've gathered, the following could work: public Comparable getParam(String param, byte[] data) { if(param.equals("some boolean variable") return data[0] != 0; else(param.equals("some float variable") { //create a new float, f, from