-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[RFC] Immutable methods #1518
Comments
Note that I don't think we should introduce without also having a direct benefit from a compiler feature this would allows us to implement. |
The main feature is being able to reason about what code modifies state. There doesn't have to be a compelling compiler feature (but there are). Lots of people are asking for immutability (#1512, #263, #65), not just in crystal but many programming languages.
I wasn't aware that |
@technorama : If Crystal were to consider this, I would much prefer effect types. See Koka's approach for an interesting take. Look at Nim's for another. A way to phrase what I am saying is: a method takes a tuple of data (of size zero or higher) as its input, answers a tuple of data (of size zero or higher) as its output, and optionally specifies a union type of effects that it can cause. About a year-and-a-half ago, I wrote my thoughts as of then. As Erik Meijer says, there is only one way to be pure, but potentially infinite ways to be impure. Haskell takes this approach. |
This is analogous to the word pure in D lang end others. |
I'd really like to see this too. Preferably I'd like all parameters to be immutable by default, and then mark mutable params with some annotation, like Otherwise, marking parameters as immutable, but default to mutable, if the other option is way out of line for Crystal. I'd also like to be able to annotate variables as immutable (set once), and I think I've already issued both these things before, so sorry if it's repeat in this comment. |
I'm closing this. A concrete algorithm is needed for this to work, for example how to detect that a function is indeed immutable/pure. But, again, this adds more syntax and semantic to the language and everyone will have to learn it. |
A method may modify any data structure (almost) anywhere in the program. Having a way to declare that a method is side effect free would aid in concurrent programming.
A new keyword
fun
could be used in place ofdef
to indicate the method is immutable.Benefits:
Examples of immutable methods:
Any of the methods below would result in a compile time error:
The text was updated successfully, but these errors were encountered: