• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

How to implement combine method which takes in another Lazy object and a BiFunction?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I have a Lazy class defined as the above. I am trying to create a combine method which takes in another Lazy object and combine it with the current Lazy object using the BiFunction to return a new Lazy object. This is what I have so far:



However, when I try to run the code, I get the error which says:

Lazy3.java:70: error: incompatible types: T#1 cannot be converted to T#2
       T thisElement = this.get();
                               ^
 where T#1,T#2 are type-variables:
   T#1 extends Object declared in class Lazy
   T#2 extends Object declared in method <T#2>combine(Lazy<T#2>,BiFunction<? su
1 error

How should I resolve this?
 
Marshal
Posts: 76405
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why have you got that boolean called isNull, which is false when your class hasn't been instantiated and true when it has? That is a sure‑fire way to confuse yourself.
You are saying that you are returning a T but in lines 26 and 30, you are trying to return the Supplier<T>. Surely you would write s.get()?
Pease explain what the purpose of the if statement is; how does it differ from line 30?
What will happen if you pass null to line 6?
 
Campbell Ritchie
Marshal
Posts: 76405
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cheryl Yeo wrote:. . . when I try to run the code . . .

Please be precise: you mean you get that error message when you try to compile the code.
Why have you declared the combine() method as a generic method?

Sorry; I didn't realise that Optional#orElseGet() is overloaded to take Supplier<? extends T> as a parameter, so I was mistaken about s.get().
 
Bartender
Posts: 5061
188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Cheryl,

the mistake is in this line:

Because of the <T> in this line, the T is now independent of the T of the class. So, leave out this <T> and your code runs fine.
 
this is supposed to be a surprise, but it smells like a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic