-
Notifications
You must be signed in to change notification settings - Fork 23
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
Change how void
parameters work
#508
Comments
This is great, I never understood why the need to consider Incidentally, if # used to return `void`
proc foo() = ...
# now becomes equivalent to
proc foo(): auto = ...
# which infers the type `void` anyway This is a common source of complaint (at least among my colleagues) - why use type inference by default for variables but not for the return type of procs? |
I would volunteer to fix all Personally, I'd like to see even more applications of But what if the user wants the sort order to depend on a function with additional state? (e.g. the user may want to sort 2D points by their distance from a given anchor point) In C++, you'll be covered because the
type
SortedSet[T, ComparisonState = void] = object
elements, etc: ...
cmpState: ComparisonState # when this is void, the field is removed
proc insert(s: SortedSet, elem: s.T) =
mixin cmp
...
if cmp(a, b, s.cmpState):
... When proc cmp(p1, p2, anchor: Point2d)
var s: SortedSet[Point2d, Point2d]
s.insert(Point2d(...))
|
@zah That is already covered by closures. If you don't like closures because they mean GC activity, there should be a different proposal to sort it out. Functors in C++ didn't save C++ from having to add lambdas either. |
@zah Nothing against |
Well, how is it covered by closures? You'll need to have an entirely different |
Can we tag the issues that motivate this with a "void" tag, so I can take a look at them? @andreaferretti,without the special rules, my nice solution above cannot work. Another nice property of the solution is that you can use the |
Please no. Doing this will affect code readability considerably, I don't want to analyse the body of a function to figure out its return type. |
Voidness of argument should not affect function arity, imo. @zah, your case can be solved as easy as: type
SortedSet[T, ComparisonState = void] = object
elements, etc: ...
cmpState: ComparisonState # when this is void, the field is removed
proc insert(s: SortedSet, elem: s.T) =
mixin cmp
...
let cmpRes = when s.cmpState is void:
cmp(a, b)
else:
cmp(a, b, s.cmpState)
if cmpRes:
... |
@zah It's not about issues - I am not aware of any in particular - it is about regularity of the language. In many other languages, not returning anything is the same as returning a Unit type, which does not have any special rules or behaviour. Of course, the backend can have special rules, like removing @dom96 Nothing would change in particular - one would still be able to decalre the return type of a function, and in particular state that it is void. It is just that the case where one wants type inference (which is very, very common) would get a slightly nicer syntax. @yglukhov +1 |
Well, yes. You can always erase |
This change is small, but it will have a massive effect. I don't want programmers to be encouraged to use type inference for this case. |
That may be your preference, but almost all programmers I know use this style regularly, especially for private functions |
How would that work with |
I don't think there would be any difference. If not specifying a return type becomes Users would still be able to |
@andreaferretti here is an annoying case with proc foo(): int {.discardable.} = discard
# Does bar return void or int?
# Currently it will return void, but with auto it will return int.
proc bar() = foo() |
I see, I did not think of this case. (in the example, I would say it does make more sense to return int, but this breaks retrocompatibility nonetheless) |
@krux02 I think you linked the wrong PR In any case - if I understand correctly the point of this issue, but @Araq can speak for himself - this is not about deprecating |
See also nim-lang/Nim#20609 |
Getting rid of the existing
void
parameter bugs is suprisingly hard. It also seems unsound from a type theory perspective, Scala uses aUnit
type instead.Steps:
void
as a parameter type.void
.Thread[void]
andFlowVar[void]
andFuture[void]
where required.void
with the empty tuple type.The text was updated successfully, but these errors were encountered: