apatrida
05/07/2021, 7:34 PMOrNull
and here is an idea to start a discussion:
https://github.com/apatrida/KEEP/blob/master/proposals/OrNull-syntactical-sugar.mdapatrida
05/07/2021, 7:35 PM*OrNull
in Kotlin runtimes (many in the generated classes), not sure what the actual count is of distinct functions. But it is a lot.apatrida
05/07/2021, 7:36 PMapatrida
05/07/2021, 7:37 PMapatrida
05/07/2021, 7:42 PMraulraja
05/08/2021, 2:55 PMval xyz = people.first { it.age < 30 } // error, is ambiguous? or default to indefinite (or definitive) variant?
Free type inference would make everything ambiguous forcing users to ascribe types everywhere to disambiguate. In this case as a user I’d prefer the compiler to infer the nullable version by default which is the safe one, but other users which prefer exception throwing api’s like first()
would prefer this to infer to a non nullable type and blow up with a domain specific exception on the illegal out of bounds access. I do like a lot the possibility to simplify the variants but not sure how to solve the default
case for resolution in a good way.
Another concern it’s this may require a strategy for backwards compatibility as it would be a breaking change.. In any case I think it’s very interesting and worth exploring. Thanks for taking the time. 🙂apatrida
05/08/2021, 4:12 PMapatrida
05/08/2021, 4:13 PMapatrida
05/08/2021, 4:14 PMapatrida
05/08/2021, 4:15 PMapatrida
05/08/2021, 4:16 PMelizarov
05/09/2021, 12:16 PMxxx()
and xxxOrNull()
pairs quite a number of times. The idea that seems to be most accepted by the team is to shorten xxxOrNull()
to xxx?()
. However, just getting a shorter name does not feel like “doing enough” to substantiate this feature. It is having to write the implementation twice that’s really ugly.
So, what we are really looking at, is a way to get rid of paired implementations, so that your can write the body of the function just once and then call it with or without ?
(in the first case it will throw function-specific exception, in the second case it will return null
). That would be a real pain-solver, but we don’t have any design on this “write once, use two-ways” idea. It is fine if the design only works for inline
functions to start with. Suggestions are welcome.Zhelenskiy
05/09/2021, 2:53 PMIt is fine is the design only works for inline functions to start with.Do you mean 'if' instead of second 'is'?
apatrida
05/10/2021, 7:09 PMxxx?
syntax in the doc as an alternative (Alt 4 - but it is on the decl and not the call site), it works, just is less noticeably but fits making a nullable function expectation. Two implementations is wrapping one call with an exception on null in the outer function, but still extra work. You thinking of an annotation to customize the exception, or just have a standard !!
type no null expected thrown.apatrida
05/10/2021, 7:11 PMapatrida
05/10/2021, 7:12 PMxxx?
is also backward compatible since you can have the compiler allow and deprecate the OrNull
form which introducing the xxx?()
call.apatrida
05/10/2021, 7:14 PM??
at the end of the call to match the !!
operator @elizarov because the xxx?(parms)
or xxx? { lambda }
just puts in a place we don't expect a null related operator like ?:
or !!
or ?.
...
xxx(params)??
is better, but then the lambda version isn't as pretty xxx { lambda }??
but at least fits with the others.elizarov
05/11/2021, 7:21 PMelizarov
05/11/2021, 7:29 PMonError
lambda that contains code on “what to do on error”. The default value for this lambda is provided by an author of the function and it throws a function-specific exception (and can even use lambda parameters to customize the message). So:
• You can call this function as xxx()
without onError
argument, and it throws an exception on error.
• You can call this function as xxx(onError = …)
with any value of this onError
lamda that you want to customize error behavior (e.g. return another value on error).
• You can call this function as xxx?()
to supply onError = { null }
argument for this lambda.apatrida
05/11/2021, 10:26 PMZhelenskiy
05/12/2021, 8:21 AMThe default value for this lambda is provided by an author of the function and it throws a function-specific exception@elizarov Is it a good to set such default? I find it error-prone because we can forget about the case when it throws an exception during writing, but we cannot forget to check the result if it is null as compiler checks.
Zhelenskiy
05/12/2021, 8:22 AMsomeFunction() : T --> someFunctionOrThrow(error = someDefault) : T
someFunctionOrNull() : T? --> someFunction() : T?
may help to reduce strangeness of some names.apatrida
05/12/2021, 7:23 PMOrThrow
reverses the behavior for backwards compatibility and breaks existing code.raulraja
05/12/2021, 8:27 PMvalue?()
could be ambiguous as that may read as invoke if not null
rather than invokeOrNull
elizarov
05/15/2021, 10:15 AMvalue?.()
syntax for the latter and, indeed it could become confusing if we end up supporting both in the future.Samuel Michael
05/17/2021, 4:25 PMelizarov
05/17/2021, 6:51 PMSamuel Michael
05/17/2021, 6:55 PMapatrida
06/05/2021, 4:13 AM