gavr123456789 / Niva

Smalltalk like programming language
https://gavr123456789.github.io/niva-site
41 stars 2 forks source link

Manual Interfaces #311

Closed gavr123456789 closed 2 weeks ago

gavr123456789 commented 2 weeks ago

Type with all members codeblock can be used as interface! Thanks @pyrotek45 for insparation nova:

struct Math {
    sq: (Int) -> Int,
}

fn math() -> Math {
    return Math {
        sq: |x: Int| x * x
    }
}

niva:

type IMath
  sq: [Int -> Int]

type Math 
Math sq: x::Int = x * x

// send "implementation" of the IMath interface 

referenceToSqExample = &Math sq:
// method that takes interface
Int mathable::IMath = [
  mathable sq: this
]

1 mathable: (IMath sq: &Math sq:)
gavr123456789 commented 2 weeks ago
type Person 
  name: String

extend Person [
  on sleep = "sleeeping" echo
  on sayHello = "Hi! I'm $name" echo
]

// interface for Person
type Human 
  yapping: []
  sleeping: []

Program h::Human = [
  h sleeping do
  h yapping do
]

p = Person name: "Alice"
pr = Program new

sayHelloReference = &p sayHello
sleepingReference = &p sleep
// human = 
pr h: (Human yapping: sayHelloReference sleeping: sleepingReference)
image
gavr123456789 commented 2 weeks ago

Creating separate references is not nesessery (niva is clojure after all) image