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 981749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:35:29+00:00 2026-05-16T04:35:29+00:00

I can see in the API docs for Predef that they’re subclasses of a

  • 0

I can see in the API docs for Predef that they’re subclasses of a generic function type (From) => To, but that’s all it says. Um, what? Maybe there’s documentation somewhere, but search engines don’t handle “names” like “<:<” very well, so I haven’t been able to find it.

Follow-up question: when should I use these funky symbols/classes, and why?

  • 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-16T04:35:29+00:00Added an answer on May 16, 2026 at 4:35 am

    These are called generalized type constraints. They allow you, from within a type-parameterized class or trait, to further constrain one of its type parameters. Here’s an example:

    case class Foo[A](a:A) { // 'A' can be substituted with any type
        // getStringLength can only be used if this is a Foo[String]
        def getStringLength(implicit evidence: A =:= String) = a.length
    }
    

    The implicit argument evidence is supplied by the compiler, iff A is String. You can think of it as a proof that A is String–the argument itself isn’t important, only knowing that it exists. [edit: well, technically it actually is important because it represents an implicit conversion from A to String, which is what allows you to call a.length and not have the compiler yell at you]

    Now I can use it like so:

    scala> Foo("blah").getStringLength
    res6: Int = 4
    

    But if I tried use it with a Foo containing something other than a String:

    scala> Foo(123).getStringLength
    <console>:9: error: could not find implicit value for parameter evidence: =:=[Int,String]
    

    You can read that error as “could not find evidence that Int == String”… that’s as it should be! getStringLength is imposing further restrictions on the type of A than what Foo in general requires; namely, you can only invoke getStringLength on a Foo[String]. This constraint is enforced at compile-time, which is cool!

    <:< and <%< work similarly, but with slight variations:

    • A =:= B means A must be exactly B
    • A <:< B means A must be a subtype of B (analogous to the simple type constraint <:)
    • A <%< B means A must be viewable as B, possibly via implicit conversion (analogous to the simple type constraint <%)

    This snippet by @retronym is a good explanation of how this sort of thing used to be accomplished and how generalized type constraints make it easier now.

    ADDENDUM

    To answer your follow-up question, admittedly the example I gave is pretty contrived and not obviously useful. But imagine using it to define something like a List.sumInts method, which adds up a list of integers. You don’t want to allow this method to be invoked on any old List, just a List[Int]. However the List type constructor can’t be so constrainted; you still want to be able to have lists of strings, foos, bars, and whatnots. So by placing a generalized type constraint on sumInts, you can ensure that just that method has an additional constraint that it can only be used on a List[Int]. Essentially you’re writing special-case code for certain kinds of lists.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was very happy to see the google code: google-api-ruby-client project, because it meant
On maps.google.com you can see a brilliant example on how a google maps application
I'm scaling a CGImageRef . I found various code examples on the web that
I'm trying to implement the 1.6 Mobile Facebook API ( http://code.google.com/p/facebook-actionscript-api/downloads/detail?name=GraphAPI_Mobile_1_6.swc ) into an
I made a mail client with the use of javamail api for android and
In my C# 3.5 program I would like to access NTFS/FAT drives from MS
Through searches, I understand the way (or, a way) to convert an Array to
Loading the image one time works as done in the following steps (for code
I've developed a Maven reporting plugin for internal use at our company. It's designed
I'm writing the bits and pieces for a ( oh no, another ) PHP

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.