Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying parametric type is not enforced by the compiler - is this a bug or is expected?

Tags:

elm

I've seen this code compile without errors and i can't say if is a bug or if its expected.

type alias Foo = List 
vs 
type alias Foo = List String

and it not just with List. Custom union types are also allowed. Ex:

type State value = Valid value | Invalid value

type alias Model1 =
    { someField : State String } -- i would say this is normal. State is a string..

type alias Model2 =
    { someField : State } -- this doesn't look right.

and also functions are allowed

function1 : List String -> Int
function1 aListOfStrings =
  1

function2 : List -> Int
function2 whatisThisNow =
  1

But if is expected - how to reason about it? I can't wrap my mind around it. Play with it here.

like image 202
AIon Avatar asked May 28 '17 13:05

AIon


People also ask

What is conformance mode in Visual Studio?

We've enabled the /permissive- conformance mode by default with new projects created in Visual C++, enabling you to write code that is much closer to C++ standards conformance. This mode disables non-conforming C++ constructs that have existed in MSVC for years.

What is type annotation in TypeScript?

Type Annotations are annotations which can be placed anywhere when we use a type. The use of Type annotation is not mandatory in TypeScript. It helps the compiler in checking the types of variable and avoid errors when dealing with the data types.

Should you type everything in TypeScript?

Yes, you should make it a habit to explicitly set all types, it's will prevent having unexpected values and also good for readability.


1 Answers

The first looks okay. Defining type alias Foo = List should allow you to use Foo instead of List. But it does not compile (with Elm 0.18):

type alias Foo = List 

names : Foo String --does not compile
names = ["a", "b"]

It seems that type aliases are not fully checked when declared, so it is possible to create type aliases that cannot be used at all.

For the first example, the compiler could be fixed to properly support it. The second example should be a compile time error though, because there is no way to ever get a value of type List (or State). Haskellers would say List (or State) has kind * -> *, but values at runtime can only have kind *.

I guess you found a bug in the current Elm version (0.18)


Interestingly, changing the above code to

type alias Foo a = List a

names : Foo String
names = ["a", "b"]
-- compiles with Elm 0.18

makes it work correctly.. The two code snippets should be equivalent though.

like image 142
Michael Avatar answered Nov 08 '22 12:11

Michael