Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Ideal Language

Name: Anonymous 2011-10-11 13:10

There a many tiresome tidbits with C++, Objective C/C++ and D, so I sat down and wrote this syntactical Idea.

Important points:

   + interface and implementation are distinct and are designed to remove repeated code pieces, such as type signatures. that also allows just having to change the types in the interface, but not in the implementation

   + readable syntax, partly borrowed from ObjC, partly from C++, partly from Go
   + no more platform-dependent cruft, thanks to modules and a "real" runtime library
   + compiletime type detection makes it possible to omit type definitions almost completely (already possible with LLVM)



/**
* #####################
* #  Class Definition #
* #####################
*/
module MyModule.Hello;

interface (class Hello)
    /* array types are declared with `[<type>]',
       instead of `<type>[]' */
    var ([Char])  name;
    /* if the function doesn't take any arguments,
       the [] can be omitted */
    func Hello;
    func ~Hello;
    func setName [(String) newname];
    func getName [] -> (String);
end;


/**
* ########################
* # Class Implementation #
* ########################
*/

import Sys;
implements MyModule.Hello;

implementation (class Hello)
    func Hello =
    {
        /* const string literals are prefixed with a '$'
           in const string literals, every character is
           treated 'literally' - hence the name.
           a string literal without a prefixed '$' is also
           a literal string, but escape sequences are
           processed in a non-const literal string.

          $"foo" could thus also be written as:
            const("foo")
        */
        Sys.LogF($"Class <%p> has been created!", Object.ToString(self));
    };

    func ~Hello =
    {
        if(self.name != nil)
        {
            delete self.name;
        }
        Sys.LogF($"Class <%p> has been destroyed!", Object.ToString(self));
    };

    func setName =
    {
        /* allocate, reallocate and deallocate are
           keywords, and can be overloaded by
           defining 'func operator allocate(Object ob, int size)'
           for all types, or 'func operator allocate(<type> ob, int size)'
           for specific types.
           even in overloaded functions, the global
           standard 'allocate' family functions are
           accessible through 'Object.alloc', 'Object.realloc',
           and 'Object.dealloc'. */
        self.name = allocate (newname.length()+1);
        /* (const-)array methods are available through the
           'Array' type, as static members. */
        Array.copy(self.name, newname, newname.length());
    };

    func getName =
    {
        /* typecasting is done via '<expr> as <type>',
           eg:
                (['h', 'i'] as String) => "hi"
                (10 as Char)           => '\n'
                (5.2 as Integer)       => 5
                (5 as Long)            => 5.0
            et cetera.

            if typecasting is not possible, eg:

                ("foo" as Char) => cannot cast a string to char

            a 'Sys.Exceptions.TypeCastError' is thrown:

                Exception 'Sys.Exceptions.TypeCastError' thrown at
                "yourfile.src", line 32213:

                    Cannot cast type 'Object.StringLiteral' to 'Object.Char'
        */
        return (self.name as String).dup();
    }

end;

/**
* #################
* # Main Function #
* #################
*/


/* as long as the function doesn't return, it doesn't need
   an explicit type either */
func main()
{
    /* similar to C#4, variables do
       not require an explicit type
    */
    var h;
    /* classes created with 'created' are allocated
       on the heap, and are similar to the
       function-style initiation of classes
       in C++. They don't need to be explicitly
       deallocated. */
    h = create Hello;
    h.setName("World");
    Sys.LogF($"Hello, %s!", h.getName());
}


Your Opinion?

Name: Anonymous 2011-10-12 3:40

The urge to create a new language is inescapable for new programmers.  It can be good to go through the exercise.  Just be aware that every new programmer has done this and that your new language is absolutely not ideal, no matter how much you love it.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List