• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Seeking elegant way to combine varying types with case-switch

 
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to design a library that takes as input arrays of varying dimension and variable type, then processes them in different pipelines according to a preferences file. The idea is that the switch method will choose one of these pipelines, and an overload will handle the varying types.

I have written some toy code that abstracts the problem. I think if I could get this code to work I would know how to design my project.

I have a base class Foo which takes an argument of either Float or Double type, and an integer for preferred pipeline. Here is the code I have written for this. Note it does not compile.



I then have two pipeline classes Bar1 and Bar2 . These both compile.



and



The desired output is

Bar 1 Double
Bar 1 Float
Bar 2 Double
Bar 2 Float

However the compiler isn't having any



I would really like to do this some more elegant way than casting the Object. Am I stuck casting the Object or is there a more elegant way to handle these cases?
 
Marshal
Posts: 80267
430
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't understand most of that code, I am afraid.
If you want to instantiate different types depending on circumstances, consider
  • 1: Giving those types a common maybe abstract supertype.
  • 2: Don't try to distinguish methods by their parameter types like that.
  • 3: Give the supertype static factory methods which can instantiate subtypes if required. It might be neater to have the subtypes as private nested classes.
  • 4: Consider passing some parameter whose value (not type) hints at the subtype required, e.g. a String or an enum element.
  • I do not believe you can pass such arrays to a switch statement because the cases have to be of type enum XYZ or int or String.
     
    Eric Barnhill
    Rancher
    Posts: 241
    Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Unfortunately the Bar methods are somehow not right. The void keyword should not be there and those are constructors not methods. When they are constructors you get the compiler message seen there. I must have posted the wrong vim register but don't see a way to edit my post.

    But, I think your answers would still be the same. It looks like the most elegant solution is probably to take an Object argument and then using getClass() cast it and pass it on to the relevant subtype and method. Thanks!
     
    Get off me! Here, read this tiny ad:
    New web page for Paul's Rocket Mass Heaters movies
    https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
    reply
      Bookmark Topic Watch Topic
    • New Topic