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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:18:19+00:00 2026-06-08T22:18:19+00:00

When do you choose to type a given function’s return type as Seq vs

  • 0

When do you choose to type a given function’s return type as Seq vs Iterable vs Traversable (or alternatively even deeper within Seq‘s hierarchy)?

How do you make that decision? We have a lot of code that returns Seqs by default (usually starting from results of a DB query and successive transformations). I tend to want to make the return types Traversable by default and Seq when specifically expecting a given order. But I don’t have a strong justification for doing so.

I am perfectly familiar with the definition of each trait, so please don’t answer with defining the terms.

  • 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-06-08T22:18:21+00:00Added an answer on June 8, 2026 at 10:18 pm

    This is a good question. You have to balance two concerns:

    • (1) try to keep your API general, so you can change the implementation later
    • (2) give the caller some useful operations to perform on the collection

    Where (1) asks you to be as little specific about the type (e.g. Iterable over Seq), and (2) asks you the opposite.

    Even if the return type is just Iterable, you can still return let’s say a Vector, so if the caller wishes to gain extra power, it can just call .toSeq or .toIndexedSeq on it, and that operation is cheap for a Vector.

    As a measure of the balance, I would add a third point:

    • (3) use a type that kind of reflects how the data is organised. E.g. when you can assume that the data does have a sequence, give Seq. If you can assume that no two equal objects can occur, give a Set. Etc.

    Here are my rules of thumb:

    • try to use only a small set of collections: Set, Map, Seq, IndexedSeq
    • I often violate this previous rule, though, using List in favour of Seq. It allows the caller to do pattern matching with the cons extractors
    • use immutable types only (e.g. collection.immutable.Set, collection.immutable.IndexedSeq)
    • do not use concrete implementations (Vector), but the general type (IndexedSeq) which gives the same API
    • if you are encapsulating a mutable structure, only return Iterator instances, the caller can then easily generate a strict structure, e.g. by calling toList on it
    • if your API is small and clearly tuned towards “big data throughput”, use IndexedSeq

    Of course, this is my personal choice, but I hope it sounds sane.

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

Sidebar

Related Questions

Is there a way (or a plugin) to make a given content type in
I have a form which allow users to choose the type of permit they
If you press Shift+Ctrl+T or choose Navigate > Open Type... you get the Open
can you just tell me for which type of requirement we should choose framework
Are there any R users aware of an "open file"-type function in R? Preferably
Given the following function templates: #include <vector> #include <utility> struct Base { }; struct
I have a form in which the user can choose a component type from
Say I want to write a function to decide whether a given integer number
I have this JSP code snippet: <%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=c%> <c:choose> <c:when test=${var1.properties[\Item Type\]
I choose an item in my list to edit (I use Context Menu) ,

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.