Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- A typical class in Mysidia Language.
- Everything is an object, every object has a class, and every language token is a first class citizen.
- All classes extends Object by default, all metaclasses extends Class by default.
- Mysidia metaclasses are created automatically with "$(classname):Class", it is also possible to define custom metaclasses.
- A namespace needs to be declared at the top of a file, followed by using statements.
- A class may implements interfaces, all interface extends Interface by default.
- Interfaces cannot have fields, they may only define method selector names, but not handler body.
- Type annotation on messages is optional, there is also optional return type specification.
- For single line method body, one can use => shortcut/syntactic sugar to make it more concise.
- When passing a message to receiver object, it is possible to pass arguments by position, or use named arguments.
- Upon receiving a message, the receiver object attempts to match it with method dictionary.
- Message keys are expressed as $(selectorName):arg1Name:arg2Name:...argnName. Hence Mysidia supports pseudo method overloading, they are in fact different methods responding to different messages.
- fields are private, methods are public, one needs to use methods to fetch field values.
- Mysidia has no class fields/messages, they are actually instance fields/messages on the metaclass.
- Exceptions may be thrown from any code, use block {} followed by a catch() method to handle them.
- Blocks are shortcut for closure, defined with { } with possible arguments wrapped in | |.
- If Blocks are passed as only argument, it is possible to omit the parenthesis().
- */
- namespace Mysidia.Std.Util;
- using Mysidia.Std.Collection.*;
- using Mysidia.Std.Lang.*;
- interface IDate{
- methods{
- getDay(), getMonth(), getYear()
- }
- }
- class Date implements IDate{
- fields{
- day = 1, mon = 1, yr = 1970
- }
- methods{
- init(day, mon, yr){
- this.day = day;
- this.mon = mom;
- this.yr = yr;
- }
- getDay() => day;
- getMonth() => mon;
- getYear() => yr;
- string toString() => "${yr}-${mon}-${day)}"; // a message with string return type
- after(Date date) => this < date;
- before(Date date) => this > date;
- }
- metaclass DateClass{
- fields{
- formatter = new DateFormatter();
- }
- methods{
- parse(string dateString) => parse(dateString, formatter);
- parse(string dateString, IDateFormatter dateFormatter){
- format = formatter.findFormat(dateString);
- format.ifNull{ throw new DateFormatException("Supplied Date Format is invalid.") };
- return format.createDate();
- }
- }
- }
- }
- date = new Date(31, 8, 2018);
- Transcript.show(date); //prints "2018-8-31"
- date2 = Date.parse("2018-08-31");
- Transcript.show(date); //prints "2018-8-31"
- date3 = { Date.parse("abcdefz") }.catch(
- on: DateFormatException,
- do: {|dfe| Transcript.show(dfe.getDetails()) }
- );
- //prints "Supplied Date Format is invalid"
- Transcript.show(date.getClass()); //prints "class: Mysidia.Std.Util.Date"
- Transcript.show(Date.getClass()); //prints "metaclass: Mysidia.Std.Util.DateClass"
- Transcript.show(Date.getClass().getClass()); //prints "metaclass: Mysidia.Std.Lang.MetaClass"
- Transcript.show(Date.getClass().getClass().getClass()); //prints "metaclass: Mysidia.Standard.Lang.MetaClass:Class"
- Transcript.show(Date.getClass().getClass().getClass().getClass()); //prints "metaclass: Mysidia.Sd.Lang.MetaClass(Metaclass is the metaclass of all metaclasses, including MetaClass:Class)"
- Transcript.show(date.getMetaClass()); //prints "metaclass: Mysidia.Std.Util.DateClass"
- Transcript.show(Date.getMetaClass()); //prints "metaclass: Mysidia.Std.Lang.MetaClass"
- Transcript.show(DateClass.getMetaClass()); //prints "metaclass: Mysidia.Std.Lang.MetaClass:Class"
- Transcript.show(MetaClass.getMetaClass()); //prints "metaclass: Mysidia.Standard.Lang.MetaClass(MetaClass is the metaclass of itself)"
- Transcript.show(MetaClass:Class.getMetaClass()); //prints "metaclass: Mysidia.Std.Lang.MetaClass:Class
- Transcript.show(Date.getSuperClass()); //prints "class: Mysidia.Std.Lang.Object"
- Transcript.show(Date.getSuperClass().getSuperClass()); //prints "class: Mysidia.Std.Lang.UndefinedObject(object has no superclass, so it returns UndefinedObject/Null)"
- Transcript.show(Date.getSuperClass().getSuperClass().getSuperClass()) //prints "class: Mysidia.Std.Lang.Object(object is the root of all classes, so naturally UndefinedObject/Null as well)"
- Transcript.show(Date.getSuperClass().getClass()); //prints "metaclass: Mysidia.Std.Lang.Object:Class"
- Transcript.show(Date.getSuperClass().getClass().getSuperClass()); //prints "class: Mysidia.Std.Lang.Class"
- Transcript.show(Date.getSuperClass().getClass().getSuperClass().getSuperClass()); //prints "class: Mysidia.Std.Lang.ClassDescription"
- Transcript.show(Date.getSuperClass().getClass().getSuperClass().getSuperClass()); //prints "class: Mysidia.Std.Lang.ClassDescription"
- Transcript.show(Date.getSuperClass().getClass().getSuperClass().getSuperClass().getSuperClass()); //prints class: Mysidia.Std.Lang.Behavior"
- Transcript.show(Date.getSuperClass().getClass().getSuperClass().getSuperClass().getSuperClass().getSuperClass()); // prints class: Mysidia.Std.Lang.Object"
- Transcript.show(Date.getInterfaces()[0]); //prints "interface: Mysidia.Std.Util.IDate"
- Transcript.show(Date.getInterface(0)); //prints "interface: Mysidia.Std.Util.IDate"
- Transcript.show(Date.getInterfaces()); //prints "Array(1): (interface: Mysidia.Std.Lang.Interface)"
- Transcript.show(IDate.getSuperClass()); //prints "interface: Mysidia.Std.Lang.Interface"
- Transcript.show(IDate.getSuperClass()); //prints "class: Mysidia.Std.Lang.Object"
- Transcript.show(IDate.getClass()); //prints "class: Mysidia.Std.Lang.IDate:Class"
- Transcript.show(IDate.getMetaClass()); //prints "metaclass: Mysidia.Std.Lang.MetaClass"
- Transcript.show(IDate:Class.getMetaClass()); //prints "metaclass: Mysidia.Std.Lang.MetaClass:Class"
- Date.toString = { "${this.mon}-${this.day}, ${this.yr}" };
- // dynamically modify the message handler toString for Date Class
- Transcript.show(date); //prints 8-31, 2018
- Date.setClass(Class:Class);
- // dynamically modify Date Object's metaclass(Date's class) to be the metaclass Class:Class
- date4 = { Date.Parse("2018-09-30") }.catch(
- on: MessageNotUnderstoodException,
- do: {|mnue| Transcript.show(mnue.getDetails()) }
- );
- //prints "Receiver Mysidia.Std.Utility.Date cannot understand message with selector 'Parse' and arguments '2018-09-30'"
- Date.init = {|mon, day, yr|
- this.mon = mon;
- this.day = day;
- this.yr = yr;
- };
- // dynamically modify the constructor/new method on class Date, it now accepts month before date
- date5 = new Date(9, 30, 2018);
- Transcript.show(date5); //prints "2018-9-30"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement