How to create a "constant reference" getter in Dart? - dart

Say I have a class ListContainer that contains and manages a list that must be accessible from outside. Since managing this list is complicated, I don't want to let anyone other than ListContainer modify it. In C++, I would create a function that returns const reference, but in Dart, const works completely differently. Just using getter will not prevent someone from modifying the list.
So how can I provide an access to the list values without allowing to modify the list?
I'm looking for something better than creating a getNth function because that would also require creating methods like length, map, and so on.

I think UnmodifiableListView is what you are looking for.
check out UnmodifiableListView Documentation
you can use it like this:
List<int> _myList = [1, 2, 3];
UnmodifiableListView<int> get myList => UnmodifiableListView(_myList);

Related

What does it mean <> in method?

I have this method
#override
Response<BodyType> convertResponse<BodyType, SingleItemType>(
Response response) {
final Response dynamicResponse = super.convertResponse(response);
final BodyType customBody =
_convertToCustomObject<SingleItemType>(dynamicResponse.body);
return dynamicResponse.replace<BodyType>(body: customBody);
}
What does it mean <BodyType> and <BodyType, SingleItemType> in this method?
These are called generics in Dart (in fact, they are called the same in other similar programming languages).
The main idea behind generics is that you could reuse the same code without relying on a specific data/return type. Imagine List in Dart. You could have a list of integers (List<int>), a list of strings (List<String>), a list of your custom objects (List<CustomType>) - the type is not hardcoded and it could be adjusted based on your needs.
Also, you could say that it would be easier just to use dynamic or Object types that would cover most of these cases. However, generics brings you type safety, and the method type itself becomes a parameter.
Here is the official documentation about generics.

Instantiating a class by String name in Dart

I am trying to call a method of a class that I only know by name as a String. Now therefore I would need a ClassMirror of that class that allowes me to instantiate an instance. However, creating ClassMirrors seems to be only possible by entering a type using reflectClass(Type) or by passing an already existing instance of that class into reflect(dynamic). So these aren`t helping if I only have a String.
In Java you can do this pretty easily, by calling Class.forName(String). Then you would get a Constructor instance, make it accessibly and call it.
Does anyone know if this is even possible in dart? What seems weird is that once you have a ClassMirror you can access fields and methods by passing symbols, which can be created by Strings.
You can put a specific list of strings to map to a specific list of closures to create a new object with specific parameters.
But you can't get a reflection without using dart:mirrors, which is being deprecated, and also had a negative impact on tree shaking to get the payload size down.
In general, you're invited to look at the package:reflectable to achieve most of what you'd want out of dart:mirrors, using source-to-source builders.

Syntax for referencing methods in Dart?

Dart supports referencing static functions, but is there a syntax to reference methods on objects as well, similar to Java/Kotlin?
[1, -3, 5].map((n) => n.abs()).forEach(print); // this works...
[1, -3, 5].map(int::abs).forEach(print); // ...but this won't compile
I said before that Dart can't do what you're trying to do, as in order to call an instance method you need an instance. That said, what you're doing is reminiscent of a Javascript coding style and isn't very idiomatic for Dart. I'd suggest the following instead:
final list = [1, -3, 5];
for (var n in list) {
print(n.abs());
}
or
final list = [1, -3, 5].map((n) => n.abs());
for (var n in list) {
print(n);
}
The forEach method isn't strictly recommended unless you have a pre-existing method you can pass in. Otherwise, it tends to make the code a bit denser and thus harder to read at a glance. (And this isn't Javascript, you don't need to worry about scope or closure issues when using a regular old for loop.)
EDIT:
There is a possible workaround, though I'm not sure I see much point in doing it this way unless it tickles a particular kind of fancy. You can "wrap" the call to the instance method within a local anonymous method, then pass the reference of that method to, in your case, the map method:
int abs(int i) => i.abs();
[1, -3, 5].map(abs).forEach(print);
In practice, this effectively just breaks the lambda method you created in your first example out into a method in its own right, so there's an argument to be made that this approach succeeds only in making the process more complicated than it needs to be.
Like it was mentioned earlier, you still need an instance to reference. If you're looking for means to shorten your code, you might want to use
[1, -3, 5].forEach((f) => print(f.abs()));

Using Dart as a DSL

I am trying to use Dart to tersely define entities in an application, following the idiom of code = configuration. Since I will be defining many entities, I'd like to keep the code as trim and concise and readable as possible.
In an effort to keep boilerplate as close to 0 lines as possible, I recently wrote some code like this:
// man.dart
part of entity_component_framework;
var _man = entity('man', (entityBuilder) {
entityBuilder.add([TopHat, CrookedTeeth]);
})
// test.dart
part of entity_component_framework;
var man = EntityBuilder.entities['man']; // null, since _man wasn't ever accessed.
The entity method associates the entityBuilder passed into the function with a name ('man' in this case). var _man exists because only variable assignments can be top-level in Dart. This seems to be the most concise way possible to use Dart as a DSL.
One thing I wasn't counting on, though, is lazy initialization. If I never access _man -- and I had no intention to, since the entity function neatly stored all the relevant information I required in another data structure -- then the entity function is never run. This is a feature, not a bug.
So, what's the cleanest way of using Dart as a DSL given the lazy initialization restriction?
So, as you point out, it's a feature that Dart doesn't run any code until it's told to. So if you want something to happen, you need to do it in code that runs. Some possibilities
Put your calls to entity() inside the main() function. I assume you don't want to do that, and probably that you want people to be able to add more of these in additional files without modifying the originals.
If you're willing to incur the overhead of mirrors, which is probably not that much if they're confined to this library, use them to find all the top-level variables in that library and access them. Or define them as functions or getters. But I assume that you like the property that variables are automatically one-shot. You'd want to use a MirrorsUsed annotation.
A variation on that would be to use annotations to mark the things you want to be initialized. Though this is similar in that you'd have to iterate over the annotated things, which I think would also require mirrors.

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