Advance functions

Multiple dispatch (Polymorphism)

Pineapple supports Multiple Dispatch, it means you can have different function with the same signature as long as they takes different types of parameters.

Multiple dispatch allows you to use Polymorphism easily without hassle.

For example:

// first plus function
def (this Number) + (that Number) -> Number
    pass

// second plus function
def (this String) + (that String) -> String
    pass

// third plus function
def (this List{Number}) + (that List{Number}) -> List{Number}
    pass


// Example of usage

12 + 13 // will call the first plus function

"12" + "13" // will call the second plus function

[1,2,3] + [4,5,6] // will call the third plus function

12 + "12" // error

Generic functions


Javascript binding

Since Pineapple is a function that compiles to Javascript, you can easily use Javascript functions within Pineapple.

To create a function that call native JS function, you need to:
- use the <javascript> tag
- prepend dollar sign $ to parameters

For example:

// Example 1
def (this Any).show
    <javascript>
    console.log($this)
    </javascript>


// Example 2
def (this Number) ^ (that Number) -> Number
    <javascript>
    Math.pow($this, $that)
    </javascript>

Not only that, it is also possible to use Node's require to import 3rd party modules.

def (filename String).open -> String
    <javascript>
    const fs = require("fs")
    return fs.readFileSync($filename);
    </javascript>