add type-unsafe 'invokeCallable' function to language module #5806
Comments
Sounds like a legit use-case to me. |
That would be helpful, I agree! |
|
And:
|
I don't immediately see what you mean by this. If it's true, then shouldn't we be addressing that problem directly? |
Well, the idea of a
|
Of course, if |
If this helps, the use case I had for this problem was to be able to pass arguments to a function when the types of both function and arguments were not known statically, but known to be "correct" at runtime... this allows me to implement a property-based test where function input is generated automatically. I can definitely see how frameworks would need to do this kind of thing in other scenarios as well... See how I use this here: |
@tombentley I see you added some commit related to this. Any chance of getting this done for 1.2.3 or do we move it to 1.3? |
An interim workaround, albeit a very slow one, which can be called like: invokeCallable(Integer.string, [50]);
invokeCallable(plus<Integer>, {50, 25}); is: shared Anything invokeCallable(
Anything(*Nothing) callable, {Anything*} arguments)
=> `function invokeCallableHelper`.invoke {
typeArguments = arguments.collect(type).reversed.fold<Type<Anything>[2]>
([`Nothing`, `[]`])(([elements, rest], element)
=> let (union = elements.union(element))
[union, `class Tuple`.apply<>(union, element, rest)])[1:1];
arguments = [
callable,
// relies on undocument property that Tuples are typed
// by their contents
arguments.fold<Anything[]>([])(uncurry(
Sequential<Anything>.withTrailing<Anything>))];
};
Anything invokeCallableHelper<Arguments>
(Anything(*Arguments) f, Arguments arguments)
given Arguments satisfies Anything[]
=> f(*arguments); |
#7019 suggest the addition of something like the
|
There appears to be no way to perform invocations on
Callable
s in a type-unsafe manner (that is, without at least having a properly typedTuple
to use as arguments) as you can withFuncitonDeclaration
s usinginvoke()
.Given that correctly typed
Tuple
s are not easy to create using the metamodel, the lack of an invoke method forCallable
s makes certain meta programming tasks difficult or impossible (see @renatoathaydes SO question).Therefore, I propose a function
ceylon.language.meta::invokeCallable()
with the following signature be added:The text was updated successfully, but these errors were encountered: