Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional strict mode for Crystal #4603

Closed
faustinoaq opened this issue Jun 22, 2017 · 6 comments
Closed

Optional strict mode for Crystal #4603

faustinoaq opened this issue Jun 22, 2017 · 6 comments

Comments

@faustinoaq
Copy link
Contributor

faustinoaq commented Jun 22, 2017

Hi people! 😄

@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 😅

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 😄

Reference: #900 and Reddit

@ysbaddaden
Copy link
Contributor

Closing as duplicate of #900.

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

@faustinoaq
Copy link
Contributor Author

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

@ysbaddaden
Copy link
Contributor

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.

@faustinoaq
Copy link
Contributor Author

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 👍

@konovod
Copy link
Contributor

konovod commented Jun 22, 2017

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.

@faustinoaq
Copy link
Contributor Author

faustinoaq commented Jun 25, 2017

Hi, @ysbaddaden !

I use show type on hover in VSCode now:

types on hover

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants