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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:31:20+00:00 2026-05-26T06:31:20+00:00

In Scala, I’d like to be able to define a generic type of abstract

  • 0

In Scala, I’d like to be able to define a generic type of abstract syntax tree nodes sort of like this

sealed abstract class AST[T <: AST[T]] {
    def child : List[T] ;
}

case class LeafAST[T <: AST[T]]( x : Int ) extends AST[T] {
    def child = Nil 
}

case class BranchAST[T <: AST[T]]( left : T, right : T ) extends AST[T] {
    def child = left :: right :: Nil
}

Now I can write generic code like this

def countLeaves[T <: AST[T]]( x : AST[T]) : Int = x match {
    case LeafAST( x ) => 1
    case BranchAST( left, right ) => countLeaves[T](left) + countLeaves[T](right) 
}

Now my first problem is that the keyword sealed seems to have no effect. If I omit a case from the match expression, there is no error. Why and how can I write what I want? (I have other problems –e.g. how to specialize AST–, but I’ll just stick to one problem per post.)

  • 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-26T06:31:20+00:00Added an answer on May 26, 2026 at 6:31 am

    There is no error — there is a warning. For example:

    scala> :paste
    // Entering paste mode (ctrl-D to finish)
    
    sealed abstract class AST[T <: AST[T]] {
        def child : List[T] ;
    }
    
    case class LeafAST[T <: AST[T]]( x : Int ) extends AST[T] {
        def child = Nil
    }
    
    case class BranchAST[T <: AST[T]]( left : T, right : T ) extends AST[T] {
        def child = left :: right :: Nil
    }
    
    // Exiting paste mode, now interpreting.
    
    defined class AST
    defined class LeafAST
    defined class BranchAST
    
    scala> def countLeaves[T <: AST[T]]( x : AST[T]) : Int = x match {
         |     case LeafAST( x ) => 1
         | }
    <console>:10: warning: match is not exhaustive!
    missing combination      BranchAST
    
           def countLeaves[T <: AST[T]]( x : AST[T]) : Int = x match {
                                                             ^
    countLeaves: [T <: AST[T]](x: AST[T])Int
    

    You can turn it into an error with the flag -Xfatal-warnings.

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

Sidebar

Related Questions

scala.math.ScalaNumber is a Java file which looks like this: public abstract class ScalaNumber extends
Scala programmer should have known that this sort of writing : class Person{ var
In Scala, I'd like to be able to write generic classes which use operators
The scala.xml package represents XML with nodes of a labelled tree. But is this
Scala Newbie alert: basically I'm trying to do something like this: where I pattern
Scala doesn't have type-safe enum s like Java has. Given a set of related
Scala Where can differences between a class and a type be observed in Scala
In Scala I can write something like this: val a = List(1, 2, 3)
In Scala, if I define a method called apply in a class or a
scala> class A { type T <: String; def f(a: T) = println(foo)} defined

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.