Is there a python dir () equivalent for a dart?

As the name says, is there an equivalent python dir() on the dart?

+4
source share
1 answer

The Python dir () function is used to determine the names that the module defines.

We can use Mirrors and write an equivalent function on our own (or at least very similar):

 import 'dart:mirrors'; List<String> dir([String libraryName]) { var lib, symbols = []; if (?libraryName) { lib = currentMirrorSystem().libraries[libraryName]; } else { lib = currentMirrorSystem().isolate.rootLibrary; } lib.members.forEach((name, mirror) => symbols.add(name)); return symbols; } 

Now here is an example:

 class Hello {} bar() => print('yay'); main() { var foo = 5; print(dir()); // [main, bar, Hello, dir] } 

Or specify a library:

 print(dir('dart:mirrors')); 

[MirroredError, TypeMirror, ObjectMirror, _LazyLibraryMirror, TypeVariableMirror, MirrorException, ClassMirror, MirrorSystem, _LocalMirrorSystemImpl, _LocalVMObjectMirrorImpl, DeclarationMirror, _LazyTypeMirror, _LocalClosureMirrorImpl, mirrorSystemOf, _LazyFunctionTypeMirror, _filterMap, MirroredCompilationError, _Mirrors, _LocalClassMirrorImpl, _LocalInstanceMirrorImpl, _LocalTypedefMirrorImpl, _LocalFunctionTypeMirrorImpl, reflects, MethodMirror, _LocalVariableMirrorImpl , LibraryMirror, _LocalIsolateMirrorImpl, FunctionTypeMirror, _LocalLibraryMirrorImpl, Mirror, _LocalObjectMirrorImpl, _LocalMirrorImpl, _makeSignatureString, _LocalTypeVariableMirrorImpl, Comment, MirroredUncaughtExceptionError, _LocalParameterMirrorImpl, _LazyTypeVariableMirror, TypedefMirror, VariableMirror, IsolateMirror, currentMirrorSystem, _dartEscape, _LocalMethodMirrorImpl, ClosureMirror, VMReference, ParameterMirror, InstanceMirror, _isSimpleValue, SourceLocation ]

This literally speaks of what was defined in a particular library (module). Now there may be some differences from the Python function, which also seems to sort names, but this should give you a start.

+8
source

All Articles