immutable class Foo
{
void bar()
{
}
}
void main()
{
auto x = new Foo();
x.bar();
// Error: function test.Foo.bar () immutable is not callable
// using argument types ()
}
What do I have to change in the program so that x.bar() compiles? Does x have the wrong type?
Looks like a bug. x is inferred to have the type Foo, which although is an immutable class, it is treated as if a mutable variable, which caused x.bar() to fail because bar() is an immutable method.
A workaround is to provide an empty immutable constructor function,
immutable class Foo
{
void bar()
{
}
immutable this() {} // <---
}
which caused the new Foo() expression to return an immutable(Foo).
Related
class Foo {
Foo(int y);
}
class Bar extends Foo {
int value;
Bar(int x) { // error in this line
value = x;
print("Hi there");
super(x); // error in this line
}
}
How can I call super inside constructor body?
Note:
I know I can use initialiser list to solve it but I would like to know how to call super inside method body?
Bar(int x): value = x, super(x); // works but I am not looking for it.
Dart does not support inheriting the constructors as explicitly callable methods. The initializer list you mentioned is the supported way to call unnamed super constructors in Dart.
However, you can achieve what you want with the help of named constructors. Take a look at the below example -
class Foo {
int superValue;
Foo(); //A default zero-argument constructor
Foo._init(this.superValue); //Named constructor
void initValue(int x) => Foo._init(x);
}
class Bar extends Foo {
int value;
Bar(int x) {
value = x;
print("Hi there");
super.initValue(x);
}
}
void main() {
Foo foo = Bar(10); //prints 'Hi there'
}
Hope it helps!
UPDATE
You can also call the super constructor and add other statements to the child constructor using this way -
class Foo {
int superValue;
Foo(this.superValue);
}
class Bar extends Foo {
int value;
Bar(int x) : super(x) {
value = x;
print("Hi there");
}
}
void main() {
Foo foo = Bar(10);
}
The problem is the following:
There is a module with such function
def testExceptions(toTest: MyClass): Unit = {
val isException = try {
toTest.increaseValue(-200);
false
} catch {
case e: Exception => true
}
if (isException) {
// some actions
} else {
// another actions
}
}
This function is importing in the file where MyClass is defined. It has such view
class MyClass(var value: Int) {
def show(): Unit = {
println(s"Value: $value")
}
def increaseValue(dv:Int): Unit = {
if(dv <= 0) { throw new IllegalArgumentException }
else { value += dv }
}
}
But I cannot pass MyClass object to the module function testExceptions because it is undefined in the file where this function exists. If I define MyClass in this file, it will be used in the testExceptions.
I will be grateful any help solving my confusion.
How to pass a class as an function argument in scala?
You can't. You can only pass objects as arguments. Classes are not objects, classes are types.
You can use reflection to get a proxy object that represents a type and pass that as an argument, though.
But I cannot pass MyClass object […]
That is because MyClass is not an object, it is a class. You cannot pass a class or any other type as an argument, only objects.
to the module function testExceptions
Note that testExceptions is defined to take an argument that is a value of type MyClass, it does not take MyClass itself as an argument. You need to instantiate an object of MyClass and pass that as an argument.
Is there a name for a pattern where the type is inferred by the context of the result type?
Eg in this example what language could i use to document the foo method and explain that a type needs to be defined for the method to work?
protocol FooType {
init()
}
func foo<T: FooType>() -> T {
return T()
}
struct Bar: FooType {
init() {
print("bar")
}
}
let bar: Bar = foo()
// works returns instance of Bar
let fooType = foo()
// fails because foo doesn't know what type to use
You don't need to document this!
Everyone that writes code in Swift knows that to call a generic function, all its type parameters must be inferred and cannot be spoon-fed like this:
foo<Bar>()
People will see foo and say, "Oh I need the compiler to infer the type for this generic parameter." They will understand what this means.
I have a class with a closure as a property:
class MyClass{
var onChange = {}
func foo(){
onChange()
}
}
A behaviour implemented in closure is used in method foo:
var c = MyClass()
c.onChange = {
println("something is changed");
}
c.foo() // prints 'something is changed'
It's easy to make closures with an argument like {(n: Int) -> Void in println(n); } but how to create a closure without input arguments?
I tried to use {}, {in}, etc., but it gave a compilation error.
How to create empty closure properly?
If I understood your question correctly, you could use:
var closure = {() -> () in
return
}
In my class, I have an array of tuples with inside a string and two pointer on method.
I don't know why, but when I want to call a method stored into my tuple I've got this error message:
Missing argument for parameter #1 in call
My class:
class toto
{
let funcs = [("a", a, aa), ("b", b, bb)]
func a()
{
}
func aa()
{
}
func b()
{
}
func bb()
{
}
func test()
{
for (key, func1, func2) in funcs
{
func1() // The error displayed: Missing argument for parameter #1 in call
}
}
}
Any suggestions ?
You have to pass a reference to self in your function call:
func1(self)()
The pointer to a class function that you store in the array doesn't include information about the instance of the class it belongs to (or better the class instance where the function is executed). In order to make it work you have to provide a context (i.e. a reference to an instance of the class), which in this case is self.
Take a look at curried functions