www.digitalmars.com         C & C++   DMDScript  

DMDScript - Adding types/classes

reply "Carlos Santander B." <csantander619 gmail.com> writes:
Walter, how could I make this work without anything else?

//-----------------
var bobby=new Dog("bobby")  //or w/o the parameter
bobby.color="black"         //or .setColor("black")
bobby.bark()
//-----------------

Without having to write any extra DMDScript code, but just by 
modifying/expanding the DMDScript source code.

Also, after that, would it be possible to write " ... new Animals.Dog(...)"?

_______________________
Carlos Santander Bernal
Feb 04 2005
parent reply "Walter" <newshound digitalmars.com> writes:
You'll have to right a new native object. A simple example is in
protoerror.d. There's also dnumber, dstring, ddate, dmath, darray for
reference.

"Carlos Santander B." <csantander619 gmail.com> wrote in message
news:cu0s1u$c6i$1 digitaldaemon.com...
 Walter, how could I make this work without anything else?

 //-----------------
 var bobby=new Dog("bobby")  //or w/o the parameter
 bobby.color="black"         //or .setColor("black")
 bobby.bark()
 //-----------------

 Without having to write any extra DMDScript code, but just by
 modifying/expanding the DMDScript source code.

 Also, after that, would it be possible to write " ... new

 _______________________
 Carlos Santander Bernal

Feb 06 2005
parent reply "Carlos Santander B." <csantander619 gmail.com> writes:
Walter wrote:
 You'll have to right a new native object. A simple example is in
 protoerror.d. There's also dnumber, dstring, ddate, dmath, darray for
 reference.
 

Thanks! I got it now. How about inheritance? Is it possible to implement? Right now, I'm duplicating behavior by adding the same functions to the NativeFunctionDefinitions (or something like that) of each derived type, but I have to remove the type check in the functions. So, is there a way? _______________________ Carlos Santander Bernal
Feb 09 2005
parent reply "Walter" <newshound digitalmars.com> writes:
"Carlos Santander B." <csantander619 gmail.com> wrote in message
news:cue6g6$g43$1 digitaldaemon.com...
 Walter wrote:
 You'll have to right a new native object. A simple example is in
 protoerror.d. There's also dnumber, dstring, ddate, dmath, darray for
 reference.

Thanks! I got it now. How about inheritance? Is it possible to implement? Right now, I'm duplicating behavior by adding the same functions to the NativeFunctionDefinitions (or something like that) of each derived type, but I have to remove the type check in the functions. So, is there a way?

Javascript has a rather unusual way of doing inheritance, you do it by setting the prototype of the object to the object you wish to inherit from.
Feb 10 2005
parent reply "Carlos Santander B." <csantander619 gmail.com> writes:
Walter wrote:
 
 Javascript has a rather unusual way of doing inheritance, you do it by
 setting the prototype of the object to the object you wish to inherit from.
 
 

Something like this? class MyClass : Dobject { static Dobject getPrototype () { //return a MyClassPrototype } } class MyClassPrototype : MyClass { ... } class MySubClass : Dobject { static Dobject getPrototype () { //return a MyClassPrototype } } I think that's ok for inheriting, but what if I want MySubClass to have new features? Is it possible to somehow mix two prototypes? What I'm doing right now is something like this: class MyClass : Dobject { static NativeFunctionData [] MyClassFunctions = ...; static Dobject getPrototype () { //return a MyClassPrototype } } class MyClassPrototype : MyClass { ... } class MySubClass : MyClass { static NativeFunctionData [] MySubClassFunctions = ...; static Dobject getPrototype () { //return a MySubClassPrototype } } class MySubClassPrototype : MySubClass { ... } And I don't add the functions in the prototype but in the object, so each subclass can see their parent's functions, and can register like that. The other issue with inheritance is when calling a member function, because in your code you have something like: void* Dboolean_prototype_toString(...) { if (!othis.isClass(TEXT_Boolean)) //error else { ... } } But now I've had to do this: void * MyClass_prototype_foo(...) { MyClass c=cast(MyClass) othis; if (!c) //error else { ... } } Is that a good approach? Is it better or worse than checking if the name is correct? _______________________ Carlos Santander Bernal
Feb 11 2005
parent reply "Walter" <newshound digitalmars.com> writes:
There's a distinction between doing a D inheritance and a script
inheritance. I think you want the script inheritance, which is done by
setting the .prototype property of the script object, not the D class. There
are some examples in the script source code. For example, in protoerror.d,
the constructor for D0_prototype sets its .prototype property to Derror's
prototype.

"Carlos Santander B." <csantander619 gmail.com> wrote in message
news:cujgul$3tr$1 digitaldaemon.com...
 Walter wrote:
 Javascript has a rather unusual way of doing inheritance, you do it by
 setting the prototype of the object to the object you wish to inherit



Something like this? class MyClass : Dobject { static Dobject getPrototype () { //return a MyClassPrototype } } class MyClassPrototype : MyClass { ... } class MySubClass : Dobject { static Dobject getPrototype () { //return a MyClassPrototype } } I think that's ok for inheriting, but what if I want MySubClass to have new features? Is it possible to somehow mix two prototypes? What I'm doing right now is something like this: class MyClass : Dobject { static NativeFunctionData [] MyClassFunctions = ...; static Dobject getPrototype () { //return a MyClassPrototype } } class MyClassPrototype : MyClass { ... } class MySubClass : MyClass { static NativeFunctionData [] MySubClassFunctions = ...; static Dobject getPrototype () { //return a MySubClassPrototype } } class MySubClassPrototype : MySubClass { ... } And I don't add the functions in the prototype but in the object, so each subclass can see their parent's functions, and can register like

 The other issue with inheritance is when calling a member function,
 because in your code you have something like:

 void* Dboolean_prototype_toString(...)
 {
 if (!othis.isClass(TEXT_Boolean))
 //error
 else
 { ... }
 }

 But now I've had to do this:

 void * MyClass_prototype_foo(...)
 {
 MyClass c=cast(MyClass) othis;
 if (!c)
 //error
 else
 { ... }
 }

 Is that a good approach? Is it better or worse than checking if the name
 is correct?

 _______________________
 Carlos Santander Bernal

Feb 11 2005
parent reply "Carlos Santander B." <csantander619 gmail.com> writes:
Walter wrote:
 There's a distinction between doing a D inheritance and a script
 inheritance. I think you want the script inheritance, which is done by
 setting the .prototype property of the script object, not the D class. There
 are some examples in the script source code. For example, in protoerror.d,
 the constructor for D0_prototype sets its .prototype property to Derror's
 prototype.
 

Well, I'm not sure I understand completely, or if I haven't explained myself correctly. I want script inheritance, but I want to do it through D code. I know it's weird since DMDScript is dynamically typed, but there's some common behavior in the classes I'm adding that is better expressed as inheritance. I'm specifically adding support for MinWin to DMDScript (and more later), so the user can write: //note: this code actually works in my sandbox var w=new MinWin.Window("foo") var b=new MinWin.Button(w,"hi") var t=new MinWin.Text(w,"hello") b.visible(true) t.visible(false) w.visible(true) "visible" is in fact a property that Window, Button and Text inherit from Component, so why should I write it more than once? But also each subclass has different properties that the others don't have. That's the specific problem I'm trying to tackle, a problem I'm currently solving as a said before, but I'd like to know if there's a better way. _______________________ Carlos Santander Bernal
Feb 11 2005
parent reply "Walter" <newshound digitalmars.com> writes:
"Carlos Santander B." <csantander619 gmail.com> wrote in message
news:cujvp9$fvr$1 digitaldaemon.com...
 Walter wrote:
 There's a distinction between doing a D inheritance and a script
 inheritance. I think you want the script inheritance, which is done by
 setting the .prototype property of the script object, not the D class.


 are some examples in the script source code. For example, in


 the constructor for D0_prototype sets its .prototype property to


 prototype.

Well, I'm not sure I understand completely, or if I haven't explained myself correctly. I want script inheritance, but I want to do it through D code. I know it's weird since DMDScript is dynamically typed, but there's some common behavior in the classes I'm adding that is better expressed as

 I'm specifically adding support for MinWin to DMDScript (and more
 later), so the user can write:

 //note: this code actually works in my sandbox
 var w=new MinWin.Window("foo")
 var b=new MinWin.Button(w,"hi")
 var t=new MinWin.Text(w,"hello")
 b.visible(true)
 t.visible(false)
 w.visible(true)

 "visible" is in fact a property that Window, Button and Text inherit
 from Component, so why should I write it more than once? But also each
 subclass has different properties that the others don't have.

 That's the specific problem I'm trying to tackle, a problem I'm
 currently solving as a said before, but I'd like to know if there's a
 better way.

Set the .prototype of the MinWin object to be the Component object.
Mar 10 2005
parent "Carlos Santander B." <csantander619 gmail.com> writes:
Walter wrote:
 
 
 Set the .prototype of the MinWin object to be the Component object.
 
 

Thanks. _______________________ Carlos Santander Bernal
Mar 11 2005