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

algorithm.nim: Use func #16304

Closed
wants to merge 6 commits into from
Closed

algorithm.nim: Use func #16304

wants to merge 6 commits into from

Conversation

ee7
Copy link
Contributor

@ee7 ee7 commented Dec 9, 2020

To confirm: do we want to change a proc that has a proc parameter into a func? (See the first commit in this PR).

#16293 was merged, so I assume the answer is "yes".

For example, this PR changes sorted from a proc to a func. However, the below code will still compile without error even when using --experimental:strictFuncs.

import algorithm

proc myCmp(x, y: int): int =
  echo "this is a side effect"
  if x == y: return 0
  if x < y: return -1
  return 1

let foo = sorted([2, 1, 3], myCmp)
echo foo

It produces:

this is a side effect
this is a side effect
this is a side effect
this is a side effect
@[1, 2, 3]

I just want to make sure that this is what we want.

See also: #16303

Copy link
Member

@timotheecour timotheecour left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I guess tests fail...

2020-12-09T17:51:16.8819350Z /Users/runner/work/1/s/lib/pure/algorithm.nim(434, 6) Error: 'sorted' can have side effects
2020-12-09T17:51:16.8836130Z an object reachable from 'a' is potentially mutated
2020-12-09T17:51:16.8863760Z /Users/runner/work/1/s/lib/pure/algorithm.nim(454, 8) the mutation is here

@ee7
Copy link
Contributor Author

ee7 commented Dec 9, 2020

hmm, I guess tests fail...

Yeah, it's due to:

cmd: "nim check --hints:on --experimental:strictFuncs --experimental:views compiler/nim.nim"

Because this:

bin/nim c --experimental:strictFuncs compiler/nim.nim

doesn't work with sorted as func. I thought I checked that, but oh well.

Let's see what happens with sorted as a proc. But I don't know if that's the right fix.

@timotheecour
Copy link
Member

I've reduced the previous CI failure to #16305

@Araq
Copy link
Member

Araq commented Dec 9, 2020

To confirm: do we want to change a proc that has a proc parameter into a func?

No, it was an oversight by me. We don't want that. Callbacks can have effects.

@timotheecour
Copy link
Member

timotheecour commented Dec 10, 2020

To confirm: do we want to change a proc that has a proc parameter into a func?

No, it was an oversight by me. We don't want that. Callbacks can have effects.

That's the crux of #16303 and IMO we do in fact want that.

proc callCb(cb: proc(): int): int = result = cb() is safe to change to
func callCb(cb: proc(): int): int = result = cb() since all potential side effects only happen through cb, as explained in #16303 (comment)

ee7 added a commit to ee7/Nim that referenced this pull request Dec 10, 2020
This commit changes the funcs that take a `proc` parameter back to
procs.

This reverts some of commit 6f57eba:
  sequtils.nim: Use `func` (nim-lang#16293)

See also:
- nim-lang#16303
- nim-lang#16304
@ee7
Copy link
Contributor Author

ee7 commented Dec 10, 2020

To confirm: do we want to change a proc that has a proc parameter into a func?

No, it was an oversight by me. We don't want that. Callbacks can have effects.

I see. I should've asked explicitly in the sequtils PR description, rather than this one. Sorry.

Questions:

  1. Could you complete this statement? "A proc in the standard library should eventually become a func if it satisfies all of the following conditions:"
  2. Does anything currently block us from changing such procs into funcs?

ee7 added a commit to ee7/Nim that referenced this pull request Dec 14, 2020
This commit changes the funcs that take a `proc` parameter back to
procs.

This reverts some of commit 6f57eba:
  sequtils.nim: Use `func` (nim-lang#16293)

See also:
- nim-lang#16303
- nim-lang#16304
Araq pushed a commit that referenced this pull request Dec 14, 2020
This commit changes the funcs that take a `proc` parameter back to
procs.

This reverts some of commit 6f57eba:
  sequtils.nim: Use `func` (#16293)

See also:
- #16303
- #16304
mildred pushed a commit to mildred/Nim that referenced this pull request Jan 11, 2021
This commit changes the funcs that take a `proc` parameter back to
procs.

This reverts some of commit 6f57eba:
  sequtils.nim: Use `func` (nim-lang#16293)

See also:
- nim-lang#16303
- nim-lang#16304
ardek66 pushed a commit to ardek66/Nim that referenced this pull request Mar 26, 2021
This commit changes the funcs that take a `proc` parameter back to
procs.

This reverts some of commit 6f57eba:
  sequtils.nim: Use `func` (nim-lang#16293)

See also:
- nim-lang#16303
- nim-lang#16304
@Araq
Copy link
Member

Araq commented Apr 21, 2021

If you want to continue with this, the next steps would be:

  • Compile talgorithm.nim with --strictFuncs:on.
  • Write tests that check wether a ref object can be used as the T in openArray[T].

@stale
Copy link

stale bot commented Apr 24, 2022

This pull request has been automatically marked as stale because it has not had recent activity. If you think it is still a valid PR, please rebase it on the latest devel; otherwise it will be closed. Thank you for your contributions.

@stale stale bot added the stale Staled PR/issues; remove the label after fixing them label Apr 24, 2022
@stale stale bot closed this May 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Staled PR/issues; remove the label after fixing them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants