inductive.js icon indicating copy to clipboard operation
inductive.js copied to clipboard

template/macro syntax for as()

Open UniqueFool opened this issue 9 years ago • 5 comments

Could you briefly explain how the macro expansion works in some of the more esoteric cases, e.g: @Map<('a | undefined)>[@String]

UniqueFool avatar May 19 '16 17:05 UniqueFool

Yeah sure - @Map is a map type. It is used to represent the hash table usage of objects in JavaScript. Example:

js

var map = { key1: 'value1', key2: 'value2' }
console.log('key1 in map?', 'key1' in map) // true
console.log('key2 in map?', 'key2' in map) // true
console.log('key3 in map?', 'key3' in map) // false

Map takes a type argument. In the example I just gave, the type would be string. So we would write this as Map<String>.

Inductive.js also supports union types. A union type is denoted like this: (Number | String). This signifies a value that can be one of several types.

Finally, inductive.js supports polymorphic types, call generic parameters in some languages. An example is an Array. Arrays can be of Numbers, Strings, or whatever. To represent this fact we have the ability to include type parameters. A type parameter looks like this: 'a. A generic array looks like Array<'a>.

In this case Map<('a | undefined)> represents a Map that takes optional 'a values, or undefined. The building block you are looking at (OptionMap.get) does a look up on the Map object for a specific key.

An example of an Map of option types might look like this:

var map = { key1: 'str1', key2: 'str2', key3: undefined }

This is a Map<String | undefined>. To look up a value we can do map['key2']. map['key2'] will return a String or undefined depending on what the map looks like.

Let me know if this is a sufficient explanation or if you need more detail.

BTW thanks for asking these questions. Until I get proper docs set up maybe I can pull some of this together in a FAQ.

omphalos avatar May 19 '16 23:05 omphalos

Until I get proper docs set up maybe I can pull some of this together in a FAQ.

I started doing that a few hours ago, when I began copying some of your answers over to the wiki - I didn't realize I could simply start a new article, but it worked: https://github.com/omphalos/inductive.js/wiki

I am hoping to add more contents over time, probably using your responses for bootstrapping new sections, and some of my own findings while tinkering with your code

UniqueFool avatar May 19 '16 23:05 UniqueFool

Wow, awesome! Thank you!

omphalos avatar May 20 '16 13:05 omphalos

I added some more of your info to the new wiki article, and realized that I ended up breaking the syntax highlighting you were using already - do you have any idea how to retain that, or what tags were you using, so I can check if they also work for the wiki ?

UniqueFool avatar May 21 '16 11:05 UniqueFool

I'm not sure how to escape markdown syntax in a comment but you can look at the raw README to see examples - it's three backticks followed by js to start a formatted JavaScript block and three backticks to close the block.

omphalos avatar May 21 '16 12:05 omphalos