Dec 27, 2018

[starlark] wrap-up

Reference:
Starlark:
https://docs.bazel.build/versions/master/skylark/language.html

Starlark github:
https://github.com/bazelbuild/starlark/

Starlark API:
https://docs.bazel.build/versions/master/skylark/lib/skylark-overview.html


Thread safe and Python syntax like language.
Design purpose is to allow Python to parse Starlark code.


Statements:
  • function definition
  • if statement
  • for loop


Types:
  • function
  • None
  • bool
  • int
  • dict
  • list
  • string
  • depset
    https://docs.bazel.build/versions/master/skylark/depsets.html
    Depsets are a specialized data structure for efficiently collecting data across a target’s transitive dependencies. Since this use case concerns the analysis phase, depsets are useful for authors of rules and aspects, but probably not macros.
    The defining feature of depset is its time- and space-efficient union operation.
    The depset constructor accepts a list of elements (“direct”) and a list of other depsets (“transitive”), and returns a depset representing a set containing all the direct elements and the union of all the transitive sets.
    Conceptually, the constructor creates a new graph node that has the direct and transitive nodes as its successors.
    Depsets have a well-defined ordering semantics, based on traversal of this graph.
  • struct - A generic object with fields. Structs fields cannot be reassigned once the struct is created.
    Two structs are equal if they have the same fields and if corresponding field values are equal.
    https://docs.bazel.build/versions/master/skylark/lib/struct.html



Evaluation contexts:
  • .bzl , (aka python module)
  • Bazel rules



Immutability:
  • list, dict mutable
  • **Once the code in a evaluation contexts is done executing, all of its values are frozen.
    i.e No python module level variable modification after module has been evaluated.
    i.e Any values that are returned from the rule’s analysis are frozen.



Differences from Python:
  • Global variables cannot be reassigned.
  • for statements are not allowed at the module-level; factor them into functions instead.
  • **Dictionaries have a deterministic order of iteration(like C++ map, sorted keys).
  • Recursion is not allowed.
  • Int type is limited to 32-bit signed integers (an overflow will throw an error).
  • Lists and other mutable types may be stored in dictionary keys once they are frozen.
  • Modifying a collection during iteration is an error. (Same as many of the C++ containers)
    Can avoid the error by iterating over a copy of the collection,
    e.g.
    for x in list(my_list):
        ....
    
    Can still modify its deep contents regardless.
  • Global (non-function) variables must be declared before they can be used in a function, even if the function is not called until after the global variable declaration.
    However, it is fine to define f() before g(), even if f() calls g().
  • The comparison operators (<, <=, >=, >) are not defined across different types of values,
    e.g., Can't compare 5 < 'foo' (however still can compare them using == or !=).
    This is a difference with Python 2, but consistent with Python 3.
    Note that this means you are unable to sort lists that contain mixed types of values.
  • Tuple syntax is more restrictive. You may use a trailing comma only when the tuple is between parentheses, e.g. write (1,) instead of 1,
  • Dictionary literals cannot have duplicated keys.
    e.g
    {"a": 4, "b": 7, "a": 1} # Error
    
  • Variable of a comprehension may not be used after the comprehension.
    This is stricter than Python 2 and Python 3, which have different behavior (shadowing vs reassignment).
  • Strings are represented with double-quotes (e.g. when you call repr).



Following Python features are not supported:
  • implicit string concatenation (use explicit + operator)
  • Chained comparisons (e.g. 1 < x < 5)
  • class (Use struct function)
  • import (Use load statement)
  • while, yield
  • float and set types
  • generators and generator expressions
  • lambda and nested functions
  • is (use == instead)
  • try, raise, except, finally (Use fail for fatal errors)
  • global, nonlocal
  • most builtin functions, most methods

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.