Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3217528
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:25:18+00:00 2026-05-17T15:25:18+00:00

I am struggling with a Scala implicit conversion problem. The following code snippet illustrates

  • 0

I am struggling with a Scala implicit conversion problem. The following code snippet illustrates my problem :

import org.junit.{ Test, Before, After };

class ImplicitsTest {

    implicit def toStringWrapper(str: String) = new StringWrapper(str);

    @Test
    def test(){
        val res1: Predicate = "str" startsWith "other";
    }

}

class StringWrapper(str: String){

    def startsWith(other: String): Predicate = null;

}

trait Predicate

How can I force the String literal “str” to be converted through the implicit conversion toStringWrapper to get startsWith return Predicate instead of Boolean?

The code example doesn’t compile. I am aware that String has already a startsWith method, I just want to use a different one, and I thought that using implicit conversions might be a way to do it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:25:18+00:00Added an answer on May 17, 2026 at 3:25 pm

    Scala thankfully doesn’t let you sneak replacement methods in without you noticing–if you call a method on a class, and the class has that method, that’s the method call you get. To do otherwise would likely cause all sorts of confusion.

    That leaves you with two other options:
    (1) Rename the method
    (2) Add a specific method to do the conversion.

    The second approach works like so: you define a class that has both the method that you want and a uniquely-named method that returns itself, and optionally an implicit conversion from that class back to string if you want to be able to use the custom item like the original string (as if it had extended String):

    object ImplicitExample {
      class CustomString(s: String) {
        def original = s
        def custom = this
        def startsWith(other: String): Int = if (s.startsWith(other)) 1 else 0
      }
      implicit def string_to_custom(s: String) = new CustomString(s)
      implicit def custom_to_string(c: CustomString) = c.original
    
      def test = {
        println("This".custom.startsWith("Thi"))
        println("This".custom.length())
      }
    }
    
    scala> ImplicitExample.test
    1
    4
    
    scala> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm struggling to separate the dependencies in the following code: public static SiteConnector ConnectToSite(String
A coworker has been struggling with this problem. The desired result is an installable
I'm struggling to implement ACL in CakePHP. After reading the documentation in the cake
I've been struggling with a problem for the past couple days and haven't found
For a while I have been struggling to integrate scala with java methods that
I am struggling to get my custom drawing code to render at the proper
Struggling to find a bit of code to easily understand. How do you add
After struggling all weekend, I finally have a sphere reflecting its environment in OpenGL.
I am struggling with a problem that In my application Im having audio files
Still struggling to understand what best practices are with respect to macros. I'm attempting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.