Skip to main content

Allow grouping

5 replies [Last post]
subanark
Offline
Joined: 2004-11-26

As cowwoc has stated grouping of exceptions, why stop there? Why not allow grouping of all types. You can speficy generic types using grouping, why not apply this to all things.
E.g.

<br />
public class GroupingTest<br />
{<br />
   private static String|StringBulider data = new StringBuilder();<br />
   /*produces the real value if data is not a String. I.e. we are not in a readonly state.*/<br />
   public static String|StringBuilder get(boolean copy)<br />
   {<br />
      if(copy)<br />
         return data.toString();<br />
      else<br />
         return data;<br />
   }<br />
   public static void main(String[] args)<br />
   {<br />
      String|StringBuilder value = get(Boolean.parse(args[0]);<br />
      //value is a CharSequence<br />
      System.out.println(value.length());<br />
   }<br />
}<br />

And & can be used to restict values.

Message was edited by: subanark

Message was edited by: subanark

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tackline
Offline
Joined: 2003-06-19

Why not replace String|StringBuilder with CharSequence? Problem solved.

There are obscure reasons why you might want more freedom to use intersection types (strongly typed handling of relations, as a seasonal example), but I can't see the overriding utilitiy of this suggestion.

subanark
Offline
Joined: 2004-11-26

I have seen many cases where a variable has two possible types and is declared as Object. Although you would still have to use instanceof it does add some meaning directly into the type. A good example of this is when a variable "could" be an array but is more likely to be a single instance.

tackline
Offline
Joined: 2003-06-19

That's a rare optimisation. Although I did do it the other day.

(Base class for sources of a particular event. Instances likely to be produced in large numbers but typically with a single listener attached. Interestingly with the listener a part of the interested class, there are no additional objects created for the observation behaviour. If you want to represent null and none distinctly, use a shared, immutable, zero-length array.)

The technique is a hack. It shouldn't have support for the language. The code should be clearly marked as a hack. Code with ugly behaviour should look ugly.

yishai
Offline
Joined: 2003-11-16

> As cowwoc has stated grouping of exceptions, why stop
> there? Why not allow grouping of all types. You can
> speficy generic types using grouping, why not apply
> this to all things.
> E.g.
> [code]
> public class GroupingTest
> {
> private static String|StringBulider data = new
> new StringBuilder();
> /*produces the real value if data is not a String.
> ng. I.e. we are not in a readonly state.*/
> public static String|StringBuilder get(boolean
> ean copy)
> {
> if(copy)
> return data.toString();
> else
> return data;
> }
> public static void main(String[] args)
> {
> String|StringBuilder value =
> alue = get(Boolean.parse(args[0]);
> //value is a CharSequence
> System.out.println(value.length());
> }
> }
> [/code]
> And & can be used to restict values.

+0.5

I don't think it is worth the complexity for a variable type, but as an extends on an interface, I could definitely see it.

[code]
public interface SpecialString extends String|StringBuilder;
[/code]

Such an interface couldn't have its own methods (hence the ; at the end) but it could merge two classes into one type, provided they didn't have incompatable method signatures.

subanark
Offline
Joined: 2004-11-26

Im not sure if its a good idea to allow refering to a method that belongs to multiple classes which is not defined in a common interface. In these cases the classes should have but did not implement an interface (or subclass) together for one reason or another.