Library written into more files - dart

I found a Dart library, which hasn't got any main lib.dart file with library 'lib'; at the beginning, but which has more files with library 'lib.sublib'; at the beginning. I understand this notation, but I have a question:
What if I want to have no main lib.dart file, but have eg three parts of my library as explained above and additionally I want to have one file, which joins all three sublibs into one for case if the user wants to use all three parts of the library?
What's right? See my two drafts below. Is any of them correct? Or should it be written anyhow else?
Draft I:
//lib.dart
library 'lib';
part 'superlib.dart';
part 'bestlib.dart';
//superlib.dart
part of 'lib';
...
//bestlib.dart
part of 'lib';
...
Draft II:
//lib.dart
library 'lib';
export 'superlib.dart';
export 'bestlib.dart';
//superlib.dart
library 'lib.super';
...
//bestlib.dart
library 'lib.best';
...

Both attempts are fine.
The first difference is that the parts can access private fields, functions, classes, class members (where the name starts with an underscore _) of each other part because they are in the same library.
The second difference is that the parts can not be imported individually by user if they are in the same library. Just the library itself could be imported.

Related

correct way to import mydart.dart file in main

In my dart project projectxyz, I have a dart class declared in in myclass.dart. In main.dart, Android Studio gives two ways, both work, but I did not understand what are the pros and cons of each method:
import 'myclass.dart';
or:
import 'package:projectxyz/myclass.dart';
What is the difference in these two approaches?
That depends on how the main file itself is invoked (and where it's located).
I'll assume the main.dart library is inside the lib/ directory, because otherwise you wouldn't have the two options for importing myclass.dart.
If you invoke the main file with a file: URI, then the relative import of myclass.dart will also be imported with a file: URI. Since Dart uses the import URI to distinguish different libraries, if someone else imports myclass.dart using a package: URI, then it will be treated as two different libraries introducing different classes with the same name.
It used to be that running dart lib/main.dart would treat that as a file: URI. The Dart parser has gotten smarter about that, and now it recognizes that an entry point library in a lib/ directory should have been a package: URI, and replaces the entry point URI with package:projectxyz/main.dart.
After that, it makes no difference whether you use myclass.dart or package:projectxyz/myclass.dart.
Really, there is no difference between the two. Saying import 'myclass.dart' is high-level sugar for import 'package:projectxyz/myclass.dart';.
On the other hand, import 'myclass.dart' is easier to read and understand, and generally looks better. It also decreases confusion as to where exactly your code is being imported from, as anybody who reads this statement knows to look for the file elsewhere in your project. Because of this, you should try to use this form wherever possible.

Dart - Need explanation of library/part and import/export

Yes, I read dart import and part of directives in same file
I have this structure:
lib/
src/
one/
SomeClass.dart
one.dart
mylib.dart
main.dart
I'm trying to achieve this behavior:
All public and hidden variables are fully accessible inside library.
All public variables from library are accessible to main.dart.
There is a problem. For some weird reason I can't use any directive with 'part of'. So I can't use this in the one.dart:
part of mylib;
import 'SomeClass.dart';
//somecode
So I either need to move class definition from SomeClass.dart to one.dart (and that will make code less readable and mixed up) or I need to move 'import' in the mylib.dart.
library mylib;
import 'SomeClass.dart';
part ..
I don't like either of the options. In the second case I will need to parse all modules then and move import/exports. Which will definitely break something.
It may sound weird but the project will build from various modules automatically. And one/ is one of them.
This app design is bad, I know. But either I need to find a better way or just make all variables public and don't bother.
Default to defining one type per file, not using part, and importing only the files you need. This covers the majority of use cases.
Now, let's say you have two types that are commonly used together - for example, a Thing and a ThingException that gets thrown when Thing does bad things. Importing both of these files everywhere is tedious, so you have three options with their own tradeoffs:
Declare both types in the same file.
Declare each type in its own file, and have the 'primary' file export the other. So, thing.dart exports thing_exception.dart. Importing thing.dart gives the importing file access to both.
Declare each type in its own file, and have the other file be a 'part of' the primary file. So, thing_exception.dart declares that it is 'part of' thing.dart. Importing thing.dart gives the importing file access to both files.
For this simple type and its exception, your best bet is to use option 1. When the amount of code grows or the two types diverge in visibility, this option becomes less attractive. This puts options 2 and 3 are on the table.
When you have separate files, option 2 is often a better approach than options 3 because you maintain some flexibility - you could only import thing_exception.dart and not thing.dart. If you use option 3, you can't do this - you either import all of the parts or none of them. This is the error you are seeing when trying to do a part and import in the same file.
Option 3 becomes valuable when you the code is in the two files is highly dependent on one another and they need the ability to access private members of each other. This is less common.
When you have a bunch of files like this together, it becomes a 'library' in the more traditional sense. You declare a main library file (your my lib.dart file) that exports files:
export 'public.dart';
export 'other_public.dart';
The bin script imports the library as a whole, but it can't see anything that isn't explicitly exported from my_lib.dart.
import 'package:mylib/mylib.dart';
Here's an example of a smallish package that uses all three of these options together for a good reference.
I think you will have better luck using import, and export with show. (Use of part of is now discouraged.)
Answers to this question may help you: When to use part/part of versus import/export in Dart?
Also the Creating library packages documentation: https://www.dartlang.org/guides/libraries/create-library-packages

gen_bridge_support ignores variadic functions

I'm trying to use a c library in RubyMotion, and in order to call out to functions in the library I need to generate a bridgesupport file. RubyMotion is requesting the generation of this file, but I can see that not a single variadic function from the library appears in the bridgesupport file. I've tried walking through the source of gen_bridge_metadata, but in the end it calls out to a parser in a shared object lib so I can't get much further than that. All I can see is that it's not declaring an AFunctionDecl for that function.
Are variadic functions just not supported full stop, or is there some sort of config that I need to apply somewhere?
So this appears to be caused by not having all the .h and .a files for the library and it's dependencies together in the same directories. Eg. I had:
/vendor/lib1
/vendor/lib2
/vendor/lib3
when I should have had
/vendor/lib3 (containing all of lib1 + lib2 as well)

The benefit of libraries over object files

I have been reading topic regarding linux libraries
http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html
it mentions:
"The benefit is that each and every object file need not be stated when linking because the developer can reference the individual library"
I am not following this statement. I wonder if someone could have a further explanation or an example please?
Thanks
It's not the best phrasing in the world, IIUC, and is a bit misleading. IMHO, instead of
The benefit is that each and every object file need not be stated when linking because the developer can reference the individual library
it should say
The benefit is that each and every object file need not be stated when linking because the developer can reference the entire library (as a named entity)
Basically, it means the following. In the absence of libraries, the author of what is now a library, could simply build a list of object files, like this:
a0.cpp -> a0.o
a1.cpp -> a1.o
...
and then she could write in the documentation "if you want functions x, y, and z", then you need to link with a3.o (because it contains x and z), a42.o (for y), but also a23.o, a15.o, and a72.o, because they contain necessary underlying parts.
This is of course unwieldy. A saner approach, as your link explains, is to create a single library from a state of common-purpose functions and classes. The instructions become "if you want the functionality of shooting up foo aliens, link with the foo_alien_shooting library".

The included part ''xclickcounter.dart'' must have a part-of directive

Create a sample web application using the Web UI (web_ui) library, e.g., mylib
open mylib.dart, make it a library:
library mylib;
import 'dart:html';
import 'package:web_ui/web_ui.dart';
part 'xclickcounter.dart';
...
open xclickcounter.dart, remove imports and insert:
part of mylib;
web/out/mylib.dart and web/out/xclickcounter.dart get messed up:
The included part ''xclickcounter.dart'' must have a part-of directive
Classes can only mixin other classes
Mixin can only be applied to class
... more errors follow
What am I doing wrong? Please help :(
Edit: if I don't edit generated sample code, wdc will generate code that falls into separate libraries:
web/out/xclickcounter.dart => x_click_counter
web/out/mylib.dart => mylib_html
Does it mean that if we use web_ui we should not create our own libraries and wdc will do this for us automatically?
Update: if I don't use any library name, similar to what generated sample code does, and only rely on the library names generated by xdc in web/out/... files, I still run into trouble when importing my two components into a 3rd file. Dart Editor will produce the following warning:
The imported libraries 'compa.dart' and 'compb.dart'
should not have the same name
The workaround is to name your libraries based on what xdc produces in web/out/... files, that is:
compa.dart => x-comp-a
compb.dart => x-comp-b
After explicitly placing components into libraries like these the Dart Editor warning disappears.

Resources