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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:59:14+00:00 2026-06-10T08:59:14+00:00

I am trying to write a function which fills an array with a given

  • 0

I am trying to write a function which fills an array with a given value using scala macros. For instance a call to:

val ary = Array( 0, 1, 2 )
fill3( ary, 50+25 )

should be expanded to:

val ary = Array(0, 1, 2 )
{
  val $value = 50+25
  ary(0) = $value
  ary(1) = $value
  ary(2) = $value       
}

Here is my first attempt:

def fill3( ary: Array[Int], x: Int ) = macro fill_impl3

def fill_impl3( c: Context )
( ary: c.Expr[Array[Int]], x: c.Expr[Int]): c.Expr[Unit] = {
  import c.universe._        
  def const(x:Int) = Literal(Constant(x))

  //Precompute x
  val valName = newTermName("$value")
  val valdef = ValDef( Modifiers(), valName, TypeTree(typeOf[Int]), x.tree )

  val updates = List.tabulate( 3 ){
  i => Apply( Select( ary.tree, "update"), List( const(i), ??? ) )
  }

  val insts = valdef :: updates
  c.Expr[Unit](Block(insts:_*))
}

But here I’m stuck for two reasons:

  1. I don’t know hot to get the precomputed value ($value)
  2. I need several of these function for Arrays of size 3, 4, 6, 9 and 27. Is there a way to dry the definitions, or should I write fill3, fill4, fill6, etc.

Is there the right way to proceed ? How can I solve my two problems ?

EDIT: I realized my initial question was stupid because the size must be known at compile time…

  • 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-10T08:59:16+00:00Added an answer on June 10, 2026 at 8:59 am
    def fill(size:Int, ary: Array[Int], x: Int ) = macro fill_impl
    
    def fill_impl( c: Context )
    (size:c.Expr[Int], ary: c.Expr[Array[Int]], x: c.Expr[Int]): c.Expr[Unit] = {
      import c.universe._        
      def const(x:Int) = Literal(Constant(x))
    
      val Literal(Constant(arySize:Int)) = size.tree
    
      //Precompute x
      val valName = newTermName("$value")
      val valdef = ValDef( Modifiers(), valName, TypeTree(typeOf[Int]), x.tree )
    
      val updates = List.tabulate( arySize ){
      i => Apply( Select( ary.tree, "update"), List( const(i), Ident(valName) ) )
      }
    
      val insts = valdef :: updates
      c.Expr[Unit](Block(insts:_*))
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to write one function which return sql statement like function get_sql($name=0,$date_start=0,$date_end=0)
I'm trying to make it so I can write a function which defines a
I'm trying to write a function that takes multiple arguments, which can come either
I'm trying to write a sync function that saves some data to nodes, which
I am trying to write a matlab mex function which uses libhdf5; My Linux
I am trying to write a simple function to load an array from XML
I am trying write a function that generates simulated data but if the simulated
I'm trying to write function to do the number_format() job for the non-ascii numbers
Trying to write a function to see how often an object exists and give
Im trying to write a function for adding category: function addCategory() { $cname =

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.