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

  • Home
  • SEARCH
  • 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 7675985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:05:43+00:00 2026-05-31T17:05:43+00:00

Im attempting to create a new operator :? on lists, which operates the same

  • 0

Im attempting to create a new operator 😕 on lists, which operates the same as :: except if the value if null, then the original list is returned. I have written the following, however it soon dawned that I didn’t really know what I was doing….

object ImplicitList {
   implicit def extendIterator[T](i : List[T]) = new ListExtension(i)

 }

 class ListExtension[T <: Any](i : List[T]) {
  def :?[B >: T] (x: B): List[B] = if (x != null) x :: i else i
 }


 final case class :?[B](private val hd: B, private val tl: ListExtension[B]) extends ListExtension[B](tl.:?(hd))
  • 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-31T17:05:44+00:00Added an answer on May 31, 2026 at 5:05 pm

    What you want is the enhance-my-library pattern. With this you can add a new method to List. Here’s how that would look:

    class EnhancedList[T](self: List[T]) {
      def ?:(t: T) = 
        t match {
          case null => self
          case _ => t :: self
        }
    }
    implicit def enhanceList[T](self: List[T]) = new EnhancedList(self)
    

    So there’s a class EnhancedList that wraps List where the new method ?: is defined, and an implicit function that converts List to EnhancedList when ?: is called. Note that you have to use ?: instead of :? because Scala’s rules are such that an operator is right-associative if and only if it ends in a :.

    Here’s how it gets used:

    scala> val l = List("a","b","c")
    l: List[java.lang.String] = List(a, b, c)
    
    scala> null ?: l
    res136: List[java.lang.String] = List(a, b, c)
    
    scala> "d" ?: l
    res137: List[java.lang.String] = List(d, a, b, c)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to create a table with an automatic column, the value of which
I am attempting to create new membership users in an Ektron CMS400.NET-based website by
I'm having a bit of confusion in attempting to retroactively create a new base
I'm fairly new to Ruby on Rails, and I'm attempting to create some fancy
I'm new to Flex, Flash, and ActionScript. I'm attempting to create a video player
I'm attempting to create a new task in the Windows Task Scheduler in C#.
I'm having some trouble with a word add-in. I'm attempting to create a new
I'm new to javascript and I am attempting to create an element with certain
I'm attempting to create a new forum on an existing forum. I can create
Hi I am attempting to create a new boostrapper from my WiX generated MSI.

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.