Comprehensive guide to Objeck's language features with examples.
class Triangle from Shape {
New() {
Parent();
}
}class Triangle from Shape implements Color {
New() {
Parent();
}
method : public : GetRgb() ~ Int {
return 0xadd8e6;
}
}
interface Color {
method : virtual : public : GetRgb() ~ Int;
}value := "Hello World!";
value->Size()->PrintLine();interface Greetings {
method : virtual : public : SayHi() ~ Nil;
}
class Hello {
function : Main(args : String[]) ~ Nil {
hey := Base->New() implements Greetings {
New() {}
method : public : SayHi() ~ Nil {
"Hey..."->PrintLine();
}
};
}
}klass := "Hello World!"->GetClass();
klass->GetName()->PrintLine();
klass->GetMethodNumber()->PrintLine();value := Class->Instance("System.String")->As(String);
value += "510";
value->PrintLine();map := Collection.Map->New()<IntRef, String>;
map->Insert(415, "San Francisco");
map->Insert(510, "Oakland");
map->Insert(408, "Sunnyvale");
map->ToString()->PrintLine();list := Collection.List->New()<IntRef>;
list->AddBack(17);
list->AddFront(4);
(list->Back() + list->Front())->PrintLine();use function Int;
class Test {
function : Main(args : String[]) ~ Nil {
Abs(-256)->Sqrt()->PrintLine();
}
}serializer := System.IO.Serializer->New();
serializer->Write(map);
serializer->Write("Fin.");
bytes := serializer->Serialize();
bytes->Size()->PrintLine();funcs := Vector->New()<FuncRef<IntRef>>;
each(i : 10) {
funcs->AddBack(FuncRef->New(\() ~ IntRef : ()
=> i->Factorial() * funcs->Size())<IntRef>);
};
each(i : funcs) {
value := funcs->Get(i)<FuncRef>;
func := value->Get();
func()->Get()->PrintLine();
};@f : static : (Int) ~ Int;
@g : static : (Int) ~ Int;
function : Main(args : String[]) ~ Nil {
compose := Composer(F(Int) ~ Int, G(Int) ~ Int);
compose(13)->PrintLine();
}
function : F(a : Int) ~ Int {
return a + 14;
}
function : G(a : Int) ~ Int {
return a + 15;
}
function : native : Compose(x : Int) ~ Int {
return @f(@g(x));
}
function : Composer(f : (Int) ~ Int, g : (Int) ~ Int) ~ (Int) ~ Int {
@f := f;
@g := g;
return Compose(Int) ~ Int;
}"Καλημέρα κόσμε"->PrintLine();
"こんにちは 世界"->PrintLine();content := System.IO.Filesystem.FileReader->ReadFile(filename);
content->Size()->PrintLine();
System.IO.Filesystem.File->Size(filename)->PrintLine();socket->WriteString("GET / HTTP/1.1\nHost:google.com\nUser Agent: Mozilla/5.0 (compatible)\nConnection: Close\n\n");
line := socket->ReadString();
while(line <> Nil & line->Size() > 0) {
line->PrintLine();
line := socket->ReadString();
};
socket->Close();pipe := System.IO.Pipe->New("foobar", Pipe->Mode->CREATE);
if(pipe->Connect()) {
pipe->ReadLine()->PrintLine();
pipe->WriteString("Hi Ya!");
pipe->Close();
};class CalculateThread from Thread {
...
@inc_mutex : static : ThreadMutex;
New() {
@inc_mutex := ThreadMutex->New("inc_mutex");
}
method : public : Run(param : System.Base) ~ Nil {
Compute();
}
method : native : Compute() ~ Nil {
y : Int;
while(true) {
critical(@inc_mutex) {
y := @current_line;
@current_line+=1;
};
...
};
}
}yesterday := System.Time.Date->New();
yesterday->AddDays(-1);
yesterday->ToString()->PrintLine();