Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changes between Version 8 and Version 9 of Tutorial

Show
Ignore:
Author:
jA_cOp (IP: 114.174.218.7)
Timestamp:
01/14/10 04:12:20 (14 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Tutorial

    v8 v9  
    5050---- 
    5151== Getting Variables == 
    52 LuaD has a special syntax for getting variables from a table, to ensure as much type safety as you want. If you want to get any variable from Lua (including ''nil''), you can do this
     52If you want to get any variable from Lua (including ''nil'') regardless of type
    5353{{{ 
    5454#!d 
    55 auto value = LuaObject in lua["SomeField"]; 
     55auto value = lua["SomeField"]; 
    5656}}} 
    5757This is the equivalent of doing the following in Lua: 
    5959local value = SomeField 
    6060}}} 
    61 Say you just wanted to see what our global variable actually was at any time; you could do: 
     61Say you just wanted to see what the value of our variable is; you could do: 
    6262{{{ 
    6363#!d 
    64 auto value = LuaObject in lua["SomeField"]; 
     64auto value = lua["SomeField"]; 
    6565Stdout(value.typeName)(": ")(value.toString).newline; 
    6666}}} 
    67 This would print the type of the object, and its value as a string. 
    68  
    69 As a random case, say we wanted to erase our global variable if it was a function: 
    70 {{{ 
    71 #!d 
    72 auto value = LuaObject in lua["SomeField"]; 
    73 if(value.isFunction) 
    74     lua["SomeField"] = nil; 
    75 }}} 
     67This would print the type of the object and its value. 
    7668 
    7769=== Type Safety === 
    78 Very often, you expect your variable to be of a certain type. Having to add checks every time to check the type of the variable would be redundant and require a lot of extra code. 
    7970 
    80 The LuaD solution is to expect a certain type when you get it from a table: 
    81 {{{ 
    82 #!d 
    83 auto func = LuaFunction in lua["SomeFunction"]; 
    84 }}} 
    85 The above errors if the our global isn't a function. Unhandled errors are thrown as exceptions of type LuaErrorException. 
    86  
    87 A typical scenario: you want to get a function and call it, as a form of callback. 
    88  
    89 That's simple enough: 
    90 {{{ 
    91 #!d 
    92 auto func = LuaFunction in lua["SomeFunction"]; 
    93 func(); 
    94 }}} 
    95 Calling a LuaFunction object works just like calling any other D function. You can of course, like in Lua, pass any and any amount of arguments to this function: 
    96 {{{ 
    97 #!d 
    98 auto print = LuaFunction in lua["print"]; 
    99 print("SomeString", 123, ["I'm a table!"]); 
    100 }}} 
    101  
    102 This is the equivalent of doing the following in Lua: 
    103 {{{ 
    104 print("SomeString", 123, {"I'm a table!"}) 
    105 }}} 
    106 Pretty easy! 
    107  
    108 ---- 
    10971== More on Tables == 
    110 So far we've only really been working with the global table. Other tables work exactly the same! 
     72So far we've only really been working with the global table. Other tables work exactly the same: 
    11173{{{ 
    11274#!d 
    11678lua["OurTable"] = table; 
    11779 
    118 //You can simplify this case as: 
     80//The above can also be written as: 
    11981 
    12082lua["OurTable"] = ["hello,", " world"]; 
    12183}}} 
    122 Pretty straight-forward so far. Everything in LuaD looks a lot like things do in Lua. 
    12384 
    12485----