Enso

Enso Project Structure

A typical Enso project structure might look like this:

my_enso_project/
├── src/
│   ├── Main.enso
│   └── MyModule.enso
├── packages/
│   └── Standard/
├── themes/
│   └── default.yaml
└── project.yaml

Basic Enso Code

Here's a simple Enso code example (Main.enso):

from Standard.Base import all

main =
    print "Hello, Enso!"
    x = 10
    y = 20
    sum = x + y
    print "The sum is {sum}"

Node.js Integration

Enso can interact with Node.js modules. Here's an example of how you might use a Node.js module in Enso:

from Standard.Base import all
import Standard.Node.Fs as Fs

main =
    content = "Hello, File!"
    Fs.writeFileSync "example.txt" content
    read_content = Fs.readFileSync "example.txt" "utf8"
    print read_content

Custom Node.js Module for Enso

You can create custom Node.js modules to use in Enso. Here's an example:

  1. Create a file named customModule.js:

// customModule.js
module.exports = {
  greet: function(name) {
    return `Hello, ${name}!`;
  },
  add: function(a, b) {
    return a + b;
  }
};
  1. Use the custom module in Enso:

from Standard.Base import all
import Standard.Node.Module as Module

main =
    custom_module = Module.require "./customModule.js"
    greeting = custom_module.greet "Enso"
    print greeting
    
    result = custom_module.add 5 7
    print "5 + 7 = {result}"

Enso Runtime in TypeScript

Enso's runtime is written in TypeScript. Here's a simplified example of how you might define a custom datatype in the Enso runtime:

// CustomType.ts
import { Atom, AtomConstructor } from "@enso-org/data-structures";

export class CustomType extends Atom {
  constructor(public value: string) {
    super();
  }

  static create(value: string): CustomType {
    return new CustomType(value);
  }

  toString(): string {
    return `CustomType(${this.value})`;
  }
}

export const CustomTypeConstructor: AtomConstructor<CustomType> = {
  create: CustomType.create,
};

Enso Language Server

Enso uses a language server for IDE integration. Here's a basic example of how you might start the Enso language server using Node.js:

// start-language-server.js
const { spawn } = require('child_process');
const path = require('path');

const serverPath = path.join(__dirname, 'enso-language-server.jar');
const server = spawn('java', ['-jar', serverPath]);

server.stdout.on('data', (data) => {
  console.log(`Server output: ${data}`);
});

server.stderr.on('data', (data) => {
  console.error(`Server error: ${data}`);
});

server.on('close', (code) => {
  console.log(`Server exited with code ${code}`);
});

Last updated