Results 1 to 3 of 3
  1. #1
    abdulsami is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Lightbulb The Proper Class Design For Designing a Custom Language Type System

    I am designing a language for my own purposes. It will have two entities basically, functions and types. e.g.

    Object1 = CreateObject1("param1", "param2", 23 ) //Line 1
    Object3 = Object1 + Object2 //Line 2
    Evaluate(Object3) //Line 3

    Line 2 evaluates if object of type Object1 be "+" to Object2 and if yes then a resultant object will be created and will be assigned to Object3. The variable definitions are like var keyword in Java Script.

    The design in my mind is like creating a base "Value" class (having primitive operations like add, subtract, multiply, divide etc) having concrete children each corresponding to different types which I plan to ingest in the language.

    class Value{
    Value add(Value value)
    ..
    }

    class Integer extends Value{
    Value add(Value value){
    //if value is compatible to add with Integer type then return the appropriate
    //resultant object else throw exception.
    }
    }

    I can create children classes like that easily but if a function changes the attributes of a object (like a member value be changed of a class) then I need to downcast to it to that type and update the appropriate property.

    class ABC extends Value{
    Value add(Value value){
    //
    }

    private int X;
    private int Y;
    private int Z;
    private string XYZ;

    public setX(int x){
    this.X = x;
    }
    .
    .
    }

    ObjectABC = GetABC();
    SetX(ObjectABC, 1)

    In the implemenatiob of the function SetX(). I will be doing something like this:

    ABC abc = (ABC)ObjectABC; //ObjectABC will be a Value type here.
    abc.setX( 1 );

    I want to get rid of this down casting thing. Can it be done? Please advise.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by abdulsami View Post
    I am designing a language for my own purposes. It will have two entities basically, functions and types. e.g.

    Object1 = CreateObject1("param1", "param2", 23 ) //Line 1
    Object3 = Object1 + Object2 //Line 2
    Evaluate(Object3) //Line 3

    Line 2 evaluates if object of type Object1 be "+" to Object2 and if yes then a resultant object will be created and will be assigned to Object3. The variable definitions are like var keyword in Java Script.
    Determining the legal types for the + operator is identical to determining the type of operands for any function, i.e. a+b is equivalent to a hypothetical add(a, b) function call.

    The type of the result is the Carthesian product of the types of operands a and b. Without knowing your language I can't be more specific.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    abdulsami is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Determining the legal types for the + operator is identical to determining the type of operands for any function, i.e. a+b is equivalent to a hypothetical add(a, b) function call.

    The type of the result is the Carthesian product of the types of operands a and b. Without knowing your language I can't be more specific.

    kind regards,

    Jos
    Well it is like

    Object1 = 1
    Object2 = 2
    Object3 = Object1 + Object2

    Here now, Object1 and Object2 are int here so the resultant should be an int here so the Object3 will have integer value of 3.

    e.g.

    class Value{
    abstract Value add(Value that);
    }

    class IntValue extends Value{
    public IntValue(int value){
    this.value = value;
    }

    int value;

    Value add(Value that){
    // It does some thing like this here
    // if( that instanceof IntValue ){
    // return new IntValue ( ((IntValue)that).value + this.value );
    // }
    // throw here
    }
    }

Similar Threads

  1. Proper generics approach for the following design
    By CChange in forum Advanced Java
    Replies: 4
    Last Post: 12-04-2010, 11:39 PM
  2. How to design this System architecture
    By thejavaman in forum Advanced Java
    Replies: 2
    Last Post: 10-18-2009, 12:41 AM
  3. System.getProperty with foreign language
    By barakz in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 04:28 AM
  4. Custom XML to Java Type Binding using JAXB
    By surajkumar in forum XML
    Replies: 0
    Last Post: 09-02-2008, 12:32 PM
  5. dentify the language type from a given String.
    By vaskarbasak in forum Advanced Java
    Replies: 18
    Last Post: 07-31-2008, 10:29 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •