You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As I've shifted to Onyx to Typescript is what Crystal to Ruby paradigm, I'd like to reiterate now on the planned type system.
Class
A class in Onyx is similar to such in Typescript.
A class shall be constructed using the new semantic to indicate that the piece of code allocates resources.
A class instance is auto-deleted once it's deemed to be not used anywhere in the program anymore.
For stage I implementation, I'm planning to use some simple GC mechanism.
Akin to Typescript, a final class instance is mutable.
A class may extend another class, a single one.
A class may be reopened later to derive new traits; new fields can't be defined, though.
That said, prefer composition over inheritance.
class Cube extend Figure { }
reopen Cube derive Drawable3D {
impl ~Drawable3D.draw(context) { } # Can implement derived methods
myCustomMethod() { } # Or define new ones!
# let foo = 42 # Can't define new fields, though
}
Struct
A struct is a passed-by-value object.
Therefore within a struct method, this is a read-only copy of the caller.
A default constructor is implicitly declared for a struct, with arguments in order of field declaration.
A struct T is constructed by simply invoking its constructor: let t = T().
It'd be panic to attempt let t = new T(); instead, use the builtin Box<T> class: let t = new Box<T>().
A final struct instance is recursively immutable.
Can always create a mutable copy, though.
struct Point {
x, y : Float64
static zero() => self(0, 0)
length() => (x ** 2 + y ** 2).sqrt()
}
final p = Point(3, 4) # Implicit constructor is defined
assert(p.length() =~ *(5, 0.01)) # NOTE: Destruction syntax, similar to `length().=~(5, 0.01)`
# p.x = 6 # => Panic! Can't mutate a `final` struct instance
let p2 = Point::zero()
p2.x = 1 # OK
Akin to class, a struct may extend another struct and be reopened.
Trait
There is no interface in Onyx to implement; instead, a trait type containing function declarations and definitions may be derived.
Note that a struct or class field may be accessed as a function (e.g. point.x()), which counts as a function implementation.
trait Scope {
decl parent(): Scope # `decl` is optional, absence of body is deemed `decl` implicitly
lookup(id: string): Entity?
find(id: string): Entity {
final found = this.lookup(id)
if (!found) throw new Error("Couldn't find")
}
}
class Block derive Scope {
final parent: Scope # Counts as a `parent()` implementation
impl lookup(id) {
final found = this.someLookup()
if (!found) found = this.parent().lookup(id) # May do `this.parent.lookup()` as well
return found
}
}
def foo(scope: Scope) {
scope.parent() # Shall be called, as declared in the trait
scope.find("Int32")
}
A trait may derive another trait.
Multiple traits may be derived by a deriver.
Enum
Unlike in Typescript, in Onyx a enum may only be an integral value.
You may define methods on a enum outside, though.
💡 A enum has undefined size and requires explicit casting to a desired type?
enum Color {
Red, # Implicitly ` = 0`
Green,
Blue
}
def Color.string() {
switch (this : Color) {
case :red: # Symbols in action!
return "Red"
case Color::Green: return "Green"
case [:blue]: {
return "Blue"
}
}
}
final color : Color = :red
assert(color as UInt8 == 0)
assert(color.string() == "Red")
assert(Color(1) == :green)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
As I've shifted to Onyx to Typescript is what Crystal to Ruby paradigm, I'd like to reiterate now on the planned type system.
Class
A
class
in Onyx is similar to such in Typescript.A class shall be constructed using the
new
semantic to indicate that the piece of code allocates resources.A class instance is auto-deleted once it's deemed to be not used anywhere in the program anymore.
For stage I implementation, I'm planning to use some simple GC mechanism.
Akin to Typescript, a
final
class instance is mutable.A class may
extend
another class, a single one.A class may be
reopen
ed later to derive new traits; new fields can't be defined, though.That said, prefer composition over inheritance.
Struct
A
struct
is a passed-by-value object.Therefore within a struct method,
this
is a read-only copy of the caller.A default constructor is implicitly declared for a struct, with arguments in order of field declaration.
A struct
T
is constructed by simply invoking its constructor:let t = T()
.It'd be panic to attempt
let t = new T()
; instead, use the builtinBox<T>
class:let t = new Box<T>()
.A
final
struct instance is recursively immutable.Can always create a mutable copy, though.
Akin to class, a struct may
extend
another struct and bereopen
ed.Trait
There is no
interface
in Onyx toimplement
; instead, atrait
type containing function declarations and definitions may bederive
d.Note that a struct or class field may be accessed as a function (e.g.
point.x()
), which counts as a function implementation.A trait may derive another trait.
Multiple traits may be derived by a deriver.
Enum
Unlike in Typescript, in Onyx a
enum
may only be an integral value.You may define methods on a enum outside, though.
A enum may
extend
another enum.A enum may be
reopen
ed as well.New enumerator definitions aren't possible, but this leaves space for method definitions!
Beta Was this translation helpful? Give feedback.
All reactions