Last active
December 8, 2016 10:03
-
-
Save lefticus/ab997fec9decf043e0b3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ChaiScript has its own simple object model and works well with object | |
// oriented C++ | |
// There is no difference between a function and a method, it's just syntactic sugar | |
var s = "mystring".size() // returns 8 | |
var s2 = size("mystring") // returns 8 | |
// class syntax | |
class MyClass | |
{ | |
// user data | |
var data | |
// constructor | |
def MyClass(x) | |
{ | |
this.data = x | |
} | |
// method | |
def getStuff(y) | |
{ | |
return this.data + y | |
} | |
} | |
var obj = MyClass(10) | |
obj.getStuff(15) // returns 25 | |
// You can extend user defined or C++ classes with additional functionality | |
def string::isStringLong() | |
{ | |
return this.size() > 10; | |
} | |
print("Hello".isStringLong()) // returns false | |
print("Hello World".isStringLong()) // returns true | |
// You can also extend the user defined type above: | |
def MyClass::getMoreStuff(d) | |
{ | |
return this.data * d | |
} | |
print(obj.getMoreStuff(10)) // returns 100 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there a difference when using
var
orattr
as a class member?