How about extending functionality of "typeof" to extract type of expression?

38 views
Skip to first unread message

Typhoon Storm

unread,
Mar 27, 2014, 11:17:47 AM3/27/14
to xtend...@googlegroups.com
Hi,

    Currently the keyword "typeof" is used to get type from a class literal. How about extending its functionality to extract type of expression?  E.g.

    val typeof(TheClass.methodWithComplicatedReturnType) result = ...
    val List<typeof(TheClass.methodWithComplicatedReturnType)> results = newArrayList

    
    This idea comes from my experience of an actual project, which is like this:

    def methodWithComplicatedReturnType() {
        return 1 -> "a" -> true -> new ArrayList<String>
    }

    def collectComplicatedResults() {
        val result = methodWithComplicatedReturnType // So far so good; the type of result is automatically inferred from the method

        val List<????> results = newArrayList // Now I want to collect multiple results into a list. 
                                              // What's type of the list......

        (0..<100).forEach[
            results.add(methodWithComplicatedReturnType)
        ]
        results.get(0). ..... // To use the elements of results, I have to know its accurate type, 
                              // which cannot be inferred directly. 
                              // Of course I can figure the type out manually, But that is cumbersome and error prone, 
                              // not to mention it has to be in sync with the return expression
    }

    Actually I figured out a workaround:

    def static <T, L extends List<T>, DUMMY> listOfType(Class<L> listClass, (DUMMY)=>T dummy) {
        return listClass.newInstance as List<T> // I want type of returned list to be List, not ArrayList
    }

    Then I can write:
    
    val results = listOfType(ArrayList) [methodWithComplicatedReturnType]

    Yes it can do the automatic type inferrence trick, but it also has flaws: It is hard to provide arguments to constructor of ArrayList, and I have to write more setOfType/mapOfType... methods for any possible structure needing complicated type as generic parameter.

    So I wonder if we can use already existent keyword "typeof" to extract type of an expression, which can be used like this:

    val typeof(TheClass.methodWithComplicatedReturnType) result = ...
    val List<typeof(TheClass.methodWithComplicatedReturnType)> results = newArrayList

    I think this can address above problem naturally. And I believe it has more power that I just yet to figure out...

Sven Efftinge

unread,
Mar 27, 2014, 11:35:43 AM3/27/14
to xtend...@googlegroups.com
Just leave out the "List<????>” part and the compiler will do the job for you.
That said, you’ll have to provide a semicolon or parenthesis after newArrayList as otherwise the parser
assumes (0..<100) to be the argument to the newArrayList call.

Sven

--
You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xtend-lang+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

signature.asc

Typhoon Storm

unread,
Mar 29, 2014, 12:17:39 AM3/29/14
to xtend...@googlegroups.com
Yes it's amazing! I thought type inference only works on the line the variable is defined.
However when I tried some more cases, I found there is still uncovered ones:


    var toReceiveResultLater // the type cannot be derived
    // after a while
    toReceiveResultLater = results.get(0) 


    var toReceiveResultLater2 = null // in this case the inferred type is null, won't it be at least Object?
    // after a while
    toReceiveResultLater2 = results.get(0) // Type mismatch: the [Accurate type of results.get(0)] is not applicable at this loctation, 
                                                            // since the inferred variable type is null, 

So maybe in these cases I have to write down the type manually.
Reply all
Reply to author
Forward
0 new messages