How to get the implementations of dart abstract classes in dart-lang github repo? - flutter

Since dart is an open source language I was trying to get the implementations of dart abstract classes? for example I would like to see how dart team implemented String abstract class to see "trim()" method implementation, there is similar question here maybe but couldn't help me.

Navigating the Dart SDK repository require some experience before it gets easier. One reason is that it contains different implementation of some methods depending on the target platform (native vs JavaScript). Also, sometimes the native implementation is being done in C++ depending on if that makes more sense.
In your case, I have found the following two implementation of String.trim(). The first is used for native (Dart VM and AOT):
https://github.com/dart-lang/sdk/blob/1278bd5adb6a857580f137e47bc521976222f7b9/sdk/lib/_internal/vm/lib/string_patch.dart#L485
And the second is used when the target is JavaScript (sometimes, we can proxy methods directly to native JavaScript methods but in this case, the trim() in JS does have a different behavior from the String.trim() in Dart. This is documented above the method in the link):
https://github.com/dart-lang/sdk/blob/1278bd5adb6a857580f137e47bc521976222f7b9/sdk/lib/_internal/js_runtime/lib/js_string.dart#L263
Note about the annotations used in the first link
The internal Dart source code can often look a bit alien compared to the normal Dart code. E.g.:
#pragma("vm:recognized", "asm-intrinsic")
#pragma("vm:external-name", "String_charAt")
external String operator [](int index);
The #pragma is used tell the Dart compiler some additional information. It can e.g. be to enforce inlining of certain methods. Or as here, tell the compiler it should call a C++ method bound to an entry called String_charAt. If we make a search in the GitHub repo. we can find that method here:
https://github.com/dart-lang/sdk/blob/e995cb5f7cd67d39c1ee4bdbe95c8241db36725f/runtime/lib/string.cc#L443-L449

Related

How to properly implement repository in flutter/dart

Being new to flutter and dart, I am looking for ways to structure my projects. I have found that the Repository pattern made its way to dart (I have a strong Java background), but I have found that there's not the necessary tools yet to properly implement a repository.
How I would normally approach this is to first create the simple straightforward methods. I ended up with the following dart code:
abstract class CategoryRepository {
Future<List<Category>> getCategories();
Future<Category?> getCategoryById(String id);
}
All is good. But now imagine I have a PostRepository, and I only want to return posts of a specific category? I am missing a standardized way in flutter how to add conditions to my repository select methods. I cannot believe nobody has written something for this purpose before so I am doubting whether I am on the right track here. Maybe my Java background is throwing me off.
In Java (specifically Spring JPA) I am used to specifications, predicates and criteria as documented here.
In .NET you can implement select methods of a repository pattern with the Expression class which allows you to use LINQ to add select criteria as documented here.
For Flutter or Dart, what would be the equivalent? Or am I on the wrong track?
I have found the flutter_repository package, but it is deprecated (without a reason). Looking at the documentation it indeed had Specifications which are used to add conditions. This would be exactly what I expect/need, but again it's deprecated.
This is where I cannot find the information publicly anymore and am questioning "how to properly implement repository in flutter/dart", hence the reason for posting this.

Can I use library keyword in personal project but not in library

I am using Flutter. As you are aware, Flutter doesn't have protected keyword to make classes package private. But interestingly, the same could be achieved using library, part, part of keywords once I name widgets starting with underscore. But, I am a bit worried about performance issues.
Question: Is it ok to use library/part/part of keywords inside ordinary projects?
public classes inside lib/src are considered package private. Only classes in lib are truly public, so are files in lib/src when they get exported by a file in lib.
enchilada/
lib/
enchilada.dart <-- public
src/
beans.dart <-- package private unless exported
While technically you can access everything in lib/src, you get a warning when you use implementation files.
Don't use part
The part keyword can not be used to hide code, it inherits the same visibility as the library. All it does is splitting a file into multiple files. It should only be used for code generation these days https://stackoverflow.com/a/27764138/669294
Note: You may have heard of the part directive, which allows you to split a library into multiple Dart files. We recommend that you avoid using part and create mini libraries instead.
source
Performance
Visibility doesn't affect performance in any way.
Naming your entity starting with an underscore (for example _test) should make it private.
Libraries not only provide APIs, but are a unit of privacy: identifiers that start with an underscore (_) are visible only inside the library. Every Dart app is a library, even if it doesn’t use a library directive.
Source

Have hidden implementation of functions in Swift like in the documentation?

I am building a rather large library and I think it would be a lot cleaner if I could have the implementation for my methods and such hidden. For example, when you view the in-code documentation for standard Swift types, such as UInt64, you see things like:
With the actual implementation of the methods hidden, and only the declarations and headers shown. How can I do this with my own library?
You need to distribute your library as a precompiled binary, in which case only the public headers + documentation will be visible for consumers of your library.
For more information, you can watch the WWDC2019 video of about Binary frameworks in Swift.
There is a shortcut in Xcode to show only the interface: Control+Command+Up Arrow, but it is a little slow and doesn't hide the internal methods that not useful for the usage of the code (since only public and open interface in Swift Package can be used by outside module).

How can I check the embedded functions definitions in swift?

I am learning Swift by myself on MacOS. When following the tutorial and practicing the array function: <array_name>.sort(), I can get it executed but I always wonder how it works and what is the actual algorithm inside it.
So is there any way to check the certain function definitions?
If you want to check the declaration of the function/class, you can command-click on it. If you want the documentation, you can either option-click it or check it on the API reference page. If you want to see the implementation details of the Swift standard library, check the source code here at GitHub. If the implementation you are checking is in a non open-source APIs though, you can't quite check it.

IntelliJ IDEA, Open API - list methods requiring be implemented

I am developing an IntelliJ IDEA plugin which has to generate some project-specific classes in the Java/Scala project. I have a superclass and a few traits to extend. How can I list methods, which I need to implement in a class being created?
I mean the same list that will appear in the 'Implement methods' dialog.
In the source code for the IDEA community version, take a look at the com.intellij.codeInsight.daemon.impl.quickfix.ImplementMethodsFix class for an example of doing this. The chooseMethodsToImplement method ultimately calls com.intellij.codeInsight.generation.OverrideImplementUtil
.showOverrideImplementChooser(...) to get the methods. Also take a look at the com.intellij.codeInsight.generation.OverrideImplementExploreUtil class.