soabase / soabase-halva

Idiomatic Scala ... in Java
https://github.com/soabase/soabase-halva/blob/master/README.md
Apache License 2.0
81 stars 5 forks source link

TypeAliases - a few more issues/thoughts from using it #24

Closed Alex-At-Home closed 8 years ago

Alex-At-Home commented 8 years ago

One of the common uses of type aliasing is to tidy up generics in local code, eg

@TypeAlias Stack extends ConsList<Integer> {}

There's a couple of issues with how that works at the moment:

1) Currently the type alias has to be declared in its own compilation unit, which makes the code much less local than you'd normally want. What would be really nice would be to be able to do something like

@TypeAliasesInside
class MyPackage {
   @TypeAlias StackAlias extends ConsList<Integer> {}

    // My business logic that actually uses `Stack`
}

which would then generate MyPackageAlias containing Stack, ie my code in MyPackage then uses final MyPackageAlias.Stack stack; (note: I think the default unsuffix/suffix should be reversed for the inner type alias, ie user types StackAlias and the compiler generates Stack).

That way all my local type aliases remain local to my package, and the codebase is more compact.

2) The type alias seems to return the original class, eg:

interface StackAlias extends ConsList<Integer> {
   //...
   // Shouldn't this return StackAlias?
   ConstList<Integer> tail(); 
}

I'm not sure that's the only reason, but one of the consequences of this is that the following code doesn't work:

   static Tuple2<Integer, StackAlias> pop(final StackAlias stack) {
        final Any<Integer> head = new AnyType<Integer>() {};
        final Any<StackAlias> tail = new AnyType<StackAlias>() {};
        return Matcher.match(stack)
                .caseOf(Any.anyHeadAnyTail(head, tail), () -> Tuple.Tu(head.val(), tail.val()))
                .get();
    }

You have to replace StackAlias with ConsList<Integer> inside the Any

Randgalt commented 8 years ago

Instead of @TypeAliasesInside this might be useful for all the generated code. So, maybe something like @HalvaContainer which would work with any of the other code generators.

Randgalt commented 8 years ago

For 2 (The type alias seems to return the original class) - should the generator always recognize the parent type and wrap the result? I guess that's possible. I need to think about that a bit.

Randgalt commented 8 years ago

Done. See here: https://github.com/Randgalt/halva/blob/master/halva/src/main/java/io/soabase/halva/container/README.md