Skip to content

Instantly share code, notes, and snippets.

@lefticus
Last active December 8, 2016 10:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lefticus/ab997fec9decf043e0b3 to your computer and use it in GitHub Desktop.
Save lefticus/ab997fec9decf043e0b3 to your computer and use it in GitHub Desktop.
// 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
@aggsol
Copy link

aggsol commented Dec 8, 2016

Is there a difference when using var or attr as a class member?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment