2

I have the following:

object T { abstract class First { def doSomething= (s:String) => Unit } class Second extends First { override def doSomething = { (s:String) => () } } def main(args: Array[String]): Unit = { new Second().doSomething } } 

but this fails to compile with the error:

Error:(8, 21) type mismatch; found : Unit required: Unit.type (s:String) => () 

Why isn't the override from class Second valid? How could I make it work?

3 Answers 3

5

The problem is that (s:String) => Unit is returning Unit.type - instead change it to (s:String) => () (or to a method if you didn't mean to be returning Function1[String, Unit] from your method.)

To put it another way:

def doSomething = (s:String) => Unit 

is really:

def doSomething: (String) => Unit.type = // A function that takes a string and returns the companion type of Unit (s: String) => Unit 

While what you probably wanted was:

def doSomething: (String) => Unit = // A function that takes a string and returns Unit (s: String) => () 

Or maybe:

def doSomething(s: String): Unit // An abstract method that takes a string and returns nothing. 
Sign up to request clarification or add additional context in comments.

5 Comments

Surely the Unit companion doesn't justify the confusion. I don't actually know why the unit value syntax () is not an alias for it. Does SIP-23 make it easier to affirm that Unit.type and ().type are the same thing in that case?
Justify, no - but for someone who is confusing T1 => T2 in type position with (v: T1) => r: T2 in value position I think String => () vs (s: String) => () // Is really ().type would be more confusing. (Unless I am mis-understanding what you mean).
Sorry that my comment wasn't directly about your good answer. Yes, you understood me: I can't wait to write def f(): () = println(), tongue in cheek. (It doesn't compile.) For now, just eliminating Unit object would avoid incidental complexity; and I wanted to express sympathy for OP.
Ah - that makes sense now, and I totally agree! Thanks for following up!
@SeanVieira sir can you help me with this stackoverflow.com/questions/53004737/…
2

(s:String) => Unit returns Unit.type ie it returns the type rather than a value of that type. You want to do (s:String) => (), which will return a value, whose type is Unit.

6 Comments

That's not accurate. Unit.type is the type of Unit's companion object, and the only value of Unit.type is the unit companion Unit.
It's not entirely inaccurate, either. (the i.e. is not precise.) hash tag conversations one should not be required to have.
@manojlds sir for below code i am getting //Error 1. '=>' expected but ']' found. 2.identifier expected but ';' found. Can you please help me to fix it
val s:String = "yes" val myFuncs: Map[String,(String) => () ]= Map( //Error 1. '=>' expected but ']' found. 2.identifier expected but ';' found. "string2" -> (() => ProcessorTwo().process(s)), "string3" -> (() => ProcessorThree().process(s)) ) myFuncs.values.foreach(v => v()); }
See below answer section for complete code ...sorry to place in answer section
|
-1
' trait Processor0 { def process = (x:String) => () } case class Processor2() extends Processor0 { override def process = (x:String) => println("Processor2 x :" + x) } case class Processor3() extends Processor0 { override def process = (x:String) => println("Processor3 x :" + x) } object DemoProc2 { def main( args : Array[String]):Unit ={ val s:String = "yes" val myFuncs: Map[String,(String) => () ]= Map( //Error 1. '=>' expected but ']' found. 2.identifier expected but ';' found. "string2" -> (() => ProcessorTwo().process(s)), "string3" -> (() => ProcessorThree().process(s)) ) myFuncs.values.foreach(v => v()); } } ' 

1 Comment

This answer surely could use some more explaining - how are you addressing the problem, what was supposed to happen, etc.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.