Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between lib/src and /bin in dart?

I know that lib/ is where we put all our library files and /bin is where we put our entrypoint for our command-line app. I know both of them are public lib/ and bin but i'm unable to understand the convention of using lib/src which according to the official docs should contain: implementation code

like image 470
Shubhamhackz Avatar asked Dec 17 '18 16:12

Shubhamhackz


People also ask

What is bin and SRC?

src files are your raw, human-readable source code (in this case, .java ). bin files are your compiled code (in this case, .class )

What is bin and lib?

"bin" is used for executables, "share" for data files, "lib" for shared libraries and so on. So if your program is a library, you can install it by default to /usr/local/lib. If it's a normal program, you can have it install to /usr/local/bin with your data files in /usr/local/share.

What is a library in DART?

A library in a programming language represents a collection of routines (set of programming instructions). Dart has a set of built-in libraries that are useful to store routines that are frequently used. A Dart library comprises of a set of classes, constants, functions, typedefs, properties, and exceptions.

What is part keyword in DART?

Dart provides the part keyword to allow you to split code into separate files in a library. It's used in the same file as the library keyword and needs to provide a relative path to the other source files that make up the library: for example, part "functions. dart"; . You can create new, empty text files for classes.


2 Answers

lib/ is the directory that contains shareable code. It can be shared

  • to other top-level directories like bin/, web/, example/, test/, tool/, ... in the same package
  • to other packages that have this package as a dependency.

lib/src by convention contains the private implementation of the public API exposed by lib/ or lib/xxx where xxx is not src.

bin is reserved for command line apps and contains the Dart entry point scripts to execute them (the files that contain main() {...}).

In pubspec.yaml you can define executables https://www.dartlang.org/tools/pub/pubspec#executables that allows you to run scripts from bin/ by just executing foo to have dart somePath/bin/foo.dart executed (using pub global activate my_package_with_foo).

like image 125
Günter Zöchbauer Avatar answered Oct 08 '22 05:10

Günter Zöchbauer


See Pub Package Layout Conventions - Implementation files

The libraries inside lib are publicly visible: other packages are free to import them. But much of a package’s code is internal implementation libraries that should only be imported and used by the package itself. Those go inside a subdirectory of lib called src. You can create subdirectories in there if it helps you organize things.

You are free to import libraries that live in lib/src from within other Dart code in the same package (like other libraries in lib, scripts in bin, and tests) but you should never import from another package’s lib/src directory. Those files are not part of the package’s public API, and they might change in ways that could break your code.

When you use libraries from within your own package, even code in src, you can (and should) still use package: to import them.

like image 44
Alexandre Ardhuin Avatar answered Oct 08 '22 04:10

Alexandre Ardhuin