vala: convenient way of getting properties - vala

I know that one can set a property of a GLib.Object-based class by name using the following simple syntax
obj["foo"] = bar;
Is there a way to get the property's value in the similar way? The following construction doesn't seem to work:
Bar bar = obj["foo"];
Vala returns error: invocation of void method not allowed as expression
I know it can be done as in the example below, but I'm looking for something conciser.
Bar bar;
obj.get("foo", out bar);

Bar bar = obj.foo;
You should use similar code to set properties, too, instead of what you wrote above:
obj.foo = bar;
It's not usually a big deal, but that form tends to be a bit more efficient than going through GObject properties. And it's shorter. IMHO it looks better, too.

Related

Defining property in constructor

I'm trying to do something that would be a basic feature in any other Object Oriented Language but for some reasons in Dart, I can't manage to do it. I'm new to Dart so this question might be dumb, but I couldn't find any answer online.
I have a property that need to be calculated once and on the constructor. This is my code so far :
class Game {
String _wordChosen;
Game() {
final _random = Random();
_wordChosen = WORDS[_random.nextInt(WORDS.length)];
}
}
WORDS is a list defined outside the class. My error is on the Game constructor :
not_initialized_non_nullable_instance_field.
I don't want to set the _wordChosen variable to a default value as that would make no sense (it would be overwritten right when the constructor is run).
I also don't want to set the property as nullable as again, it would make no sense.
i think the answer is using the keyword late to make compiler know that you will initialize the variable before using it but not now like below
late String _wordChosen;
i think this is your solution and it in null safety documents here
i hope this answer helps you

Binding boolean (Property) to List operations in JavaFX

I found JavaFX to be suprisingly expressive after getting over the fact that I had to redeclare every field as property so I am a little stumped and would like to know if there is a better, more idomatic way of binding a boolean to some more complex operation like adding and removing style classes.
In this example I have a project that maybe valid or not and I want the font color of the tab to indicate which it is. I am doing this by adding and removing the error style class:
val errorSwitch = { valid : Boolean ->
logger.debug {"errorSwitcher: $valid"}
if( valid) tab.styleClass.remove("error")
else tab.styleClass.add("error")
Unit
}
product.isValid.onChange (errorSwitch)
errorSwitch(product.isValid.value)
What I don't like here is that I have to call the function once myself to start with because "onChange" obviously does not trigger unless the isValid actually changes. It's a small thing but I am wondering if there isn't a better way with bindings because thats what I want: the presence of the error class should be bound to "isValid"
In TornadoFX the best way to achieve this is to use the toggleClass function, which will automatically add or remove a given class based on a boolean observable value. Therefore you can simply say:
tab.toggleClass(Styles.error, product.isValid)
This example requires you to use the Type Safe CSS feature of TornadoFX. Read more about Type Safe CSS in the guide :)

What are the use cases of multilevel Symbols?

Glossary: What I mean by multilevel Symbol is something that is like #Dummy.bar instead of single level like #bar. Dont know what is better name for it, sorry.
Symbols are very nifty. I can do:
class Foo {
static String hello() => "Said hello!";
}
ClassMirror clMir = reflectType(Foo) as ClassMirror();
print(clMir.invoke(#hello, []));
It prints Said hello!. Great!
But one cant do:
class Bar{
static final Bar bar = new Bar();
}
ClassMirror clMir = reflectType(Bar) as ClassMirror();
print(clMir.invoke(#bar.hello, []));
This would fail.
Also, I haven't found a place where you can get reflection for multilevel Symbol, like reflectSymbol(#Dummy.bar) and get InstanceMirror for bar or something similar.
So are there use cases for multilevel Symbols?
There are different use-cases.
Primarily, it represents a library name (library foo.bar.baz;). The mirror system uses it for that (see, e.g., https://api.dartlang.org/stable/1.20.1/dart-mirrors/MirrorSystem/findLibrary.html).
Secondarily, the mirror system is sometimes using it as a "qualified name" like library.name.ClassName.memberName to represent an exact class or library member (https://api.dartlang.org/stable/1.20.1/dart-mirrors/DeclarationMirror/qualifiedName.html).
It's not that useful outside of the mirror system.

Can I define setter and getter in the attribute itself, rather than the containing class?

Suppose I want my class to do things on attribute access. I can of course do that in setters and getters:
class Foo {
set bar (v) {
// do stuff
}
}
However, if I want to attach the same behavior to multiple attributes, I'd have to explicitly define the same setters and getters for every one of them. (The use case I have in mind is an observable, i.e. a class that knows when its attributes are being changed).
What I'd like to do is something like:
class Foo {
var bar = new AttributeWithAccessBehavior();
}
Python does this with descriptors - what is the closest thing in Dart?
AFAIK there isn't anything with getter/setter syntax that you can reuse.
You could assign a function to a field, that you can access using call notation (), but you have to be careful to call the function instead of overriding the field assignment.
A similar but more powerful alternative are classes that can emulate functions (see https://www.dartlang.org/articles/emulating-functions/)
A class that has a call method can be used like a method.
This is similar to assigned functions mentioned above but in addition you can store state information.
If you implement actual getter/setter you can of course delegate to whatever you want, but that is obviously not what you are looking for.
For the use case you mentioned, there is the observe package.
I have no idea how exactly it solves the problem, but it works quite well.

What is the syntax for implicit cast operator in dart?

I would like to cast instances of my custom class A to int. What is the syntax of the implicit cast operator? (I thought I remembered that there is such a feature but I can't find it on the web)
int a = (new A());
You can also use as to help tell the tools "no, really, treat this object as this type".
A good example of this is when you have to deal with dart:html's querySelector() function.
FormElement form = querySelector('#sign-up') as FormElement;
In the above, the object returned by querySelector('#sign-up') is checked that it is really an instance of FormElement.
Learn more at https://www.dartlang.org/docs/dart-up-and-running/ch02.html#operators
Type annotations are not allowed to affect behavior in Dart. If you're not running in checked mode, then this:
int a = new A();
will work the same as this:
var a = new A();
at run-time. In other words, when not in checked mode, you're welcome to store your A in a variable annotated as an int, but no actual conversion takes place.
If you are running in checked mode, the first form will give you a runtime exception.
I'm not sure, but I think what you're asking for is a way to define a conversion between your class A and int that will happen automatically when "cast" to an int. No such thing exists, to my knowledge. You should simply define a method to do so. For example:
int a = new A().to_i();

Resources