How to turn a Ceylon Sequential or array into a common Tuple with the appropriate type?

I have a generic function that needs to create a Tuple to call a function whose arguments I do not know.

Something like this (except for array in this example, it is created by some external code, so I can’t just apply the function directly):

 Result apply<Result, Where>( Anything[] array, Callable<Result, Where> fun) given Where satisfies Anything[] => nothing; 

Is there a safe type to implement this method and get the function that needs to be called with these arguments?

+5
source share
2 answers

This cannot be made completely type safe ... but assuming the array does contain elements of the correct types, since they must appear in a Tuple of type Where , the following function will do the trick:

 Tuple<Anything, Anything, Anything> typedTuple({Anything+} array) { if (exists second = array.rest.first) { return Tuple(array.first, typedTuple({ second }.chain(array.rest.rest))); } else { return Tuple(array.first, []); } } 

And the application is applied as:

 Result apply<Result, Where>( [Anything+] array, Callable<Result, Where> fun) given Where satisfies Anything[] { value tuple = typedTuple(array); assert(is Where tuple); return fun(*tuple); } 
+2
source

Nothing has to do with the array type with fun parameters, so the signature cannot be implemented in a safe way. You do not limit the type of array at all; it can contain anything. How, in principle, does a type implementation safely handle the case when fun expects [String, Integer] , but array is [Boolean+] ?

0
source

Source: https://habr.com/ru/post/1237525/


All Articles