Skip to content
This repository has been archived by the owner on Oct 18, 2021. It is now read-only.

Tricks and tips

Martin Takáč edited this page Mar 14, 2021 · 1 revision

Hello world

open import "prelude.ml"
let () =
	put_line "Hello, world!"

Imperative code

open import "prelude.ml"
let () =
	(put_line "Une")
	(put_line "Deux")
	(put_line "Trois")

Insert lua code

unit means that we will not return any value. _ indicates that we will not check the type of argument. The Amulet checks only the syntax of the lua code. Otherwise we can create anything there.

external val foo : _ -> unit =
	"function(src) \
		print(src) \
	end"

Sharing lua code

external val string_extensions : {} -> unit =
	"function(_) \
		string.x_shift_path = function(path) \
			local index = path:find('/') \
			local curr = path \
			local tail = nil \
			if index then \
				curr = path:sub(0, index - 1) \
				tail = path:sub(index + 1) \
			end \
			return curr, tail \
		end \
	end"

external val x_first : string -> string =
	"function(str) \
		local curr, _ = str:x_shift_path() \
		return cur \
	end"

let () =
	(string_extensions {})
	put_line (x_first "path/to/anything")

Creating typed code from lua

type color =
	| Red
	| Orange
	| Green

external val makeColor : string -> color =
	"function(s) \
		if s == 'red' then \
			return {__tag = \"Red\"} \
		end \
		if s == 'orange' then \
			return {__tag = \"Orange\"} \
		end \
		return {__tag = \"Green\"} \
	end"

{{draft}}