Crystal: Optional strict mode for Crystal

Created on 22 Jun 2017  路  6Comments  路  Source: crystal-lang/crystal

Hi people! :smile:

@asterite I'm working with crystal code programmed by others developers, I think would be very useful to have optionally a --strict flag that allow to check that all types need to be explicit.

I got this idea because working with TypeScript projects is very awesome, not because the syntax but because Types help a lot when you need understand the type that a function needs to return by example.

def process_data(data)
  # do some cool stuff
end
def process_data(data : NotificationData) : ResponseData
   # do some cool stuff
end

I'm not saying to make types mandatory but something like use strict in JavaScript, Also don't need to add var or let or const keywords, instead when use --strict flag crystal checks that variables have typed specified:

Crystal normal mode crystal build myprogram.cr

a = 10
puts a #=> 10

Crystal strict mode _optional_ crystal build --strict myprogram.cr

a = 10
puts a # Type Error: in strict mode you need to specify types

Then you try again with type specified crystal build --strict myprogram.cr:

a : Int32
a = 10
puts a #=> 10

All ok!

I really like type inference and write code without types but sometimes is useful to have Types in complex projects. The --strict mode proposal is not mandatory, just an tool to help to the programmer to remember where a type is missed. In fact the --strict mode could live inside crystal tool strict or something similar allowing to check strict mode in some files only checking that all code have types.

I know that crystal is strict about types but I'm saying that --strict flag do compiler just more strict :sweat_smile:

Extract from Reddit by @RX14 :

If you typo a variable, most of the time you will get a compile error

Yes, but I would wish don't wait until compile time to know what type a function must return.

Please let me now your opinions :smile:

Reference: #900 and Reddit

Most helpful comment

Hi, @ysbaddaden !

I use show type on hover in VSCode now:

types on hover

Is some slow in my computer :sweat_smile: (Old Intel Celeron) but works!

All 6 comments

Closing as duplicate of #900.

This is the exact opposite of crystal goals: not having to bother with types (much).

Ok @ysbaddaden :sweat_smile: Do you know if exist some way to implement this using a shard or macros?

Well, you can use the parser, and from there check wheher as a type definition. But I really don't see the point.

What I would like, however, is a tool to printout inferred types. Something similar to crystal tool hierarchy but for methods arguments and return types.

What I would like, however, is a tool to printout inferred types. Something similar to crystal tool hierarchy but for methods arguments and return types.

Yes, this is a better option. I had not thought!,

Thanks you @ysbaddaden :+1:

Reddit link is about completely different suggestion - don't enforce types declaration, enforce variable declaration (differentiate it from variable assignment, but still let it be untyped). Personally i would like such feature, but yes, perhaps it's too late and it just don't fit in a language.

Hi, @ysbaddaden !

I use show type on hover in VSCode now:

types on hover

Is some slow in my computer :sweat_smile: (Old Intel Celeron) but works!

Was this page helpful?
0 / 5 - 0 ratings