2013年12月3日 星期二

dart 語言心得

Comments:
單行  //
多行  /*...*/
document comments
    ///
    /**
    內容中含有 [xxx] , xxx參考到 class/method/field/top-level variable/function/parameter


Two runtime modes:
  production - faster, deploy (ignore assert statements and static type checking)
  checked - helpful development


Variable default value
  Uninitialized variables have an initial value of null.
  int linecnt;  <<-- linecnt is null by default, not 0


Optional type system
  it is optional to declare the type of variables/functions/parameters/fields.
  if you declare it, it is useful to type checking on checked runtime mode


Final and Const
  final variable is initialized the first time it's used, it can set only once.
    (Lazy initialization of final variables helps apps start up faster)
  const variables is compile-time constants.
    const pi=3.14159;
    const round=2*pi;


Build-in Types
  numbers: int (任何長度的整數) / double(凡含小數點的數字) / num
  strings: String
  booleans: bool (true/false)
  lists: List<T> / Collection
  maps: Map

String (immutable sequences of UTF-16 code units)
  quote / double-quote / triple-quote for multi-lines string
  raw string ==> r"In a raw string, even \n isn't special."
  字串比較用 == (是比較字串內容)

Boolean
   In Dart, only the value true is treated as true, all other values are treated as false.
   check string if empty ==> str.isEmpty()
   check int if NaN  ==> iVar.isNaN()

List (zero-based indexing)
   In Dart, array is List object.
   var list = [1,2,3,4];

Map
    用宣告方式產生
    var abc = {
        'test1' : [123,456,789],
        'test2' : [666,777]
    };
    用 constructor產生
    var def = new Map();
    var nnn = new Map<int, String>();



Function
  if no return value is specified, the statement return null.

Optional parameters (can be either positional or named, but not both)
  default values must be compile-time constants such as literals. If no default value, the value is null.
  checking caller passing value for an optional parameter: ?param
   if (?device) { return true if the caller specified the parameter
      //....
   }

  Named parameters use {param1, param2,...} to declare
    enableFlags( {bool bold, bool hidden : false} ) {
      // ...
    }

  In caller side, named parameters using paramName : value
    enableFlags(bold: true, hidden: false);

  Positional parameters use [] to declare it
    String say( String from, String msg, [String device="carrier", String mood] ) {
    }



Type Test Operators
  as Typecast
  is  True if the object has the specified type
  is! False if the object has the specified type




沒有留言:

張貼留言