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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:14:31+00:00 2026-06-05T13:14:31+00:00

I’m not sure if i got the topic right. I’ll try to describe the

  • 0

I’m not sure if i got the topic right. I’ll try to describe the problem.
I have one common field trait. StringField and IntField extend this class:

trait BaseField[T] {
  def name = "field"

  var owner : FieldContainer

  var value : T
  def set(value : T) {
    this.value = value
    this.owner.fields.put(name, this)
  }
}


class StringField extends BaseField[String]
class IntField extends BaseField[Int]

How do i implement the FieldContainer class? What i want is to match the FieldTypes later on:

val fieldContainer = {...init code here...}

fieldContainer.fields foreach {
  field -> {
     field match {
       case f: StringField => println("String")
       case f: IntField => println("Int")
       case _ => println("Unknown")
     }
  }
}

This is my FieldContainer (so far)

trait FieldContainer {

  private metaFields : HashMap[String, Any] = new HashMap[String, Any]
  def fields : HashMap[String, Any] = this.metaFields
}

And i use it in that way:

class Pizza extends FieldContainer {

  object name extends StringField(this) {
    override def name = "pizza_name"
  }

  object pieces extends IntField(this) {
     override def name = "pieces_count"
  }
}
  • 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-05T13:14:32+00:00Added an answer on June 5, 2026 at 1:14 pm

    Fields don’t need to know their owners.

    class BaseField[T](initValue: T, val name: String = "field") {
      private[this] var _value: T = initValue
      def apply() = _value
      def update(v: T) { _value = v }
      override def toString(): String = name + "(" + apply() + ")"
    }
    
    class StringField(initValue: String, name: String = "field") extends BaseField[String](initValue, name)
    class IntField(initValue: Int, name: String = "field") extends BaseField[Int](initValue, name)
    
    trait FieldContainer {
      protected def addField[C <: BaseField[_]](field: C): C = {
        _fields += (field.name -> field)
        field
      }
    
      protected def stringField(initValue: String, name: String): StringField =
        addField(new StringField(initValue, name))
    
      protected def intField(initValue: Int, name: String): IntField =
        addField(new IntField(initValue, name))
    
      private var _fields : Map[String, Any] = Map[String, Any]()
      def fields : Map[String, Any] = _fields
    }
    

    Objects (singletons) initialized when first accessed, so you should use val instead of object for fields:

    class Pizza extends FieldContainer {
    
      val name = stringField("", "pizza_name")
    
      val pieces = intField(0, "pieces_count")
    
      val mass: BaseField[Double] = addField(new BaseField[Double](0, "mass"))
    }
    

    Usage:

    scala> val p = new Pizza()
    p: Pizza = Pizza@8c61644
    
    scala> p.fields
    res0: Map[String,Any] = Map(pizza_name -> pizza_name(), pieces_count -> pieces_count(0), mass -> mass(0.0))
    
    scala> p.name() = "new name"
    
    scala> p.pieces() = 10
    
    scala> p.mass() = 0.5
    
    scala> p.fields
    res4: Map[String,Any] = Map(pizza_name -> pizza_name(new name), pieces_count -> pieces_count(10), mass -> mass(0.5))
    
    scala> p.name()
    res5: String = new name
    
    scala> p.pieces()
    res6: Int = 10
    
    scala> p.mass
    res7: BaseField[Double] = mass(0.5)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I have been unable to fix a problem with Java Unicode and encoding. The
this is what i have right now Drawing an RSS feed into the php,
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

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.