wende / elchemy

Write Elixir code using statically-typed Elm-like syntax (compatible with Elm tooling)

Home Page:https://wende.github.io/elchemy/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add custom type inference

ShalokShalom opened this issue · comments

Hi there :)

I read and hear and experience all over the place, that custom types provide more security to our code, since build-in data types such as Int, String and so on can convolute quite quickly.

I can have dozens of them in my code and mixing them is not really a good idea.
This does, in my opinion, contradict the sense of typing.

Why not make all types by default types of their own, if not defined otherwise?
This does also reduce the clutter in our code.
Like so:

main = 
  Browser.Sandbox
    { init = init 
    , view = view 
    , update = update 
    }

Becomes this:

main = 
  Browser.Sandbox
    { init 
    , view 
    , update
    }

And the compiler infers the same type as above, simply based on the name.
This also encourages people to use custom types, we could say this is the default way.

It is also simpler to read in type annotations then.

That doesn't look like custom type inference, but rather looks like punning.

In which way? I simply see no reason to repeat myself, when I want to use the same name for the type and the actual content. I think it is sane to default to informative types, instead of type declarations like String -> String -> String. Sooner or later, you lose the overview with such generic type names, at least I do. Why not infer them, when I don't annotate them specifically? It simply means less typing and less clutter.

In your example though of this:

main = 
  Browser.Sandbox
    { init = init 
    , view = view 
    , update = update 
    }

Being able to be written as:

main = 
  Browser.Sandbox
    { init 
    , view 
    , update
    }

There are no types specified here at all, rather it is a main function declaration building a record with 3 keys of names init/view/update, which are likely names of other functions either in or imported into this same file. As they are bindings to names of functions or values then it is punning yes?

yes, I mean in regular records. This is a rather bad example. You get the idea.