Variable splatting

Variable splatting is a useful technique for assigning several values at once. When you specify several variable names near the var keyword and an array expression at left hand side, each value will get a corresponding item from the array assigned to it. Consider the following code:

type foo
  static string[] bar
    return new [ "simon" "kamina" "yoko" ]

var dude1, dude2, girl = foo:bar
print dude1 // "simon"
print dude2 // "kamina" 

If the array does not contain enough values, the variables will be set with default values (false for bool, 0 for int, 0.0 for float, 0+0j for complex and null for any other type. If there are more values than needed, extra ones are simply discarded.

Last edited Mar 14, 2011 at 8:33 PM by impworks, version 1


No comments yet.