HyukjinKwon commented on a change in pull request #25736: [SPARK-29026][SQL] 
Improve error message in `schemaFor` in trait without companion object 
constructor
URL: https://github.com/apache/spark/pull/25736#discussion_r322538435
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/ScalaReflection.scala
 ##########
 @@ -906,7 +906,19 @@ trait ScalaReflection extends Logging {
    * only defines a constructor via `apply` method.
    */
   private def getCompanionConstructor(tpe: Type): Symbol = {
-    
tpe.typeSymbol.asClass.companion.asTerm.typeSignature.member(universe.TermName("apply"))
+    def throwUnsupportedOperation = {
+      throw new UnsupportedOperationException(s"Unable to find constructor for 
$tpe. " +
+        s"This could happen if $tpe is an interface, or a trait without 
companion object " +
+        "constructor.")
+    }
+    val companionSym = tpe.typeSymbol.asClass.companion match {
+      case NoSymbol => throwUnsupportedOperation
+      case sym => sym
+    }
 
 Review comment:
   No big deal but I think you can just combine those two pattern matches:
   
   ```scala
   tpe.typeSymbol.asClass.companion match {
     case NoSymbol =>
       throw new UnsupportedOperationException(s"Unable to find constructor for 
$tpe. " +
         s"This could happen if $tpe is an interface, or a trait without 
companion object " +
         "constructor.")
     case sym => sym.asTerm.typeSignature.member(universe.TermName("apply")) 
match {
       case NoSymbol =>
         throw new UnsupportedOperationException(s"Unable to find constructor 
for $tpe. " +
           s"This could happen if $tpe is an interface, or a trait with 
companion object " +
           "but that companion has no constructor and apply method.")
       case constructorSym => constructorSym
     }
   }
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to